当项目中需要用到地点导航,可能你需要集成三方地图,但现在更多的应用的做法是直接调起手机中已经安装好的地图,高德,百度等等,这样比集成三方地图来得更快,可行性也更高,下面代码都是参考网络上的,自己整理了下
一.使用谷歌地图
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q=31.1198723,121.1099877(上海青浦大街100号)")); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); startActivity(intent);
二、使用三方地图
判断地图应用是否安装
private boolean isInstallByread(String packageName) { return new File("/data/data/" + packageName).exists(); }
private void openGaode(){ try { if (isInstallByread("com.autonavi.minimap")) { Intent intent = new Intent( "android.intent.action.VIEW", android.net.Uri.parse( "androidamap://route?sourceApplication=应用名称" + "&dlat="+ currentLatitude//终点的经度 + "&dlon="+ currentLongitude//终点的纬度 +"&dname=终点地名" + "&dev=0" + "&t=1")); startActivity(intent); } else { Toast.makeText(MainActivity.this, "没有安装百度地图客户端,请先下载该地图应用", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } }
2.百度地图
private void openBaidu(){ try { if (isInstallByread("com.baidu.BaiduMap")) { Intent intent = new Intent(); intent.setData(Uri.parse("baidumap://map/direction?origin=name:我的位置|latlng:" +locLongitude//起始点经度 +"," +locLatitude//起始点纬度 +"&destination=" +currentLatitude//终点纬度 +"," +currentLongitude//终点经度 +"&mode=transit&sy=0&index=0&target=1")); intent.setPackage("com.baidu.BaiduMap"); startActivity(intent); // 启动调用 } else { Toast.makeText(MainActivity.this, "没有安装百度地图客户端,请先下载该地图应用", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } }
个人测试的时候仅测试了下高德和百度的都可以,需要注意的是高德地图的经纬度和百度地图的经纬度存在差异,也就是说,假如你的项目使用高德api获取到的经纬度,不能直接拿到百度地图来进行导航,需要进行转换,代码可以网上查,挺多,反之亦然。谷歌地图的启动的时候一直提示网络无法连接,因为是外网,需要翻墙,估计在项目中用到不多,手机上如果没有谷歌地图的话,启动会报ActivityNotFoundException,try catch下就好了,代码仅供参考,有建议或意见欢迎提出
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。