用com.blankj.utilcode.util.AppUtils工具类先检测是否安装了高德或百度地图。
if(AppUtils.isAppInstalled("com.autonavi.minimap")) {
} else if(AppUtils.isAppInstalled("com.baidu.BaiduMap")) {
}
高德和百度都提供了对应的文档,用来给外部APP去导航和查位置等功能。
可以自行查看对应的地址【高德】、【百度】
下面贴出我的代码
fun openNavigation(startAddress: String,startLatitude: Double, startLongitude: Double,
endAddress: String,endLatitude: Double, endLongitude: Double) {
if(AppUtils.isAppInstalled("com.autonavi.minimap")) {
val intent = Intent()
intent.setData(
Uri.parse("androidamap://route?sourceApplication=shangyun&slat="
+ startLatitude + "&slon=" + startLongitude + "&sname="+startAddress+"&dlat=" + endLatitude + "&dlon="
+ endLongitude + "&dname="+endAddress+ "&dev=0&m=0&t=0&showType=1"));
startActivity(intent)
}else if(AppUtils.isAppInstalled("com.baidu.BaiduMap")) {
val intent = Intent()
intent.setData(Uri.parse("baidumap://map/direction?region="
+ "杭州" + "&origin=name:" + startAddress + "|latlng:"
+ startLatitude + "," + startLongitude + "&destination=name:" + endAddress + "|latlng:" + endLatitude
+ "," + endLongitude + "&mode=driving"))
startActivity(intent)
}
}