Ⅰ.前言
接到産品新需求,模仿高德地圖APP通過搜尋關鍵詞,在地圖示識附近區域内 "關鍵詞"所搜到的點,地圖和清單之間有抽屜似的效果,具體的看下面效果圖。而打開第三方地圖APP進行導航,也是産品要求的一個小功能,寫完~記錄下
效果圖:
Ⅱ.實作
參考官方文檔
- 百度: http://lbsyun.baidu.com/index.php?title=uri/api/android
- 高德: https://lbs.amap.com/api/amap-mobile/guide/android/route
- 騰訊: https://lbs.qq.com/uri_v1/guide-mobile-navAndRoute.html
判斷本地是否安裝第三方App
通過以下函數
fun isInstallByread(String packageName): Boolean {
return File("/data/data/${packageName}").exists();
}
應用包名:
- 百度:com.baidu.BaiduMap
- 高德:com.autonavi.minimap
- 騰訊:com.tencent.map
實作 :
/**調起第三方地圖APP導航*/
fun openMapToDaoHan(packageName: String, toLatLng: LatLng, context: Context?){
var showToastTxt: String = ""
try {
when (packageName) {
OpenMapAppDialog.PACKAGE_NAME_BAIDU -> {
showToastTxt = "手機未安裝百度地圖APP"
val intent = Intent()
//導航界面
intent.setData(Uri.parse("baidumap://map/direction?destination=latlng:${toLatLng.latitude},${toLatLng.longitude}|name:目的地&coord_type=bd09ll&mode=driving"))
//由于沒擷取到目的地位址,是以跳到目的地界面
//intent.setData(Uri.parse("baidumap://map/geocoder?location=${item?.la},${item?.lg}&src=andr.baidu.openAPIdemo"))
context?.startActivity(intent)
}
OpenMapAppDialog.PACKAGE_NAME_GAODE -> {
showToastTxt = "手機未安裝高德地圖APP"
val intent = Intent()
intent.setPackage("com.autonavi.minimap")
intent.setAction(Intent.ACTION_VIEW)
intent.addCategory(Intent.CATEGORY_DEFAULT)
val destination = convertBaiduToGPS(toLatLng);//轉換坐标系
intent.setData(Uri.parse("androidamap://route?sourceApplication=${GlobalUtils.getString(R.string.app_name)}&" +
"dlat=" + destination.latitude + "&dlon=" + destination.longitude + "&dname=目的地" + "&dev=0&t=0"))
context?.startActivity(intent)
}
OpenMapAppDialog.PACKAGE_NAME_TENGXUN -> {
showToastTxt = "手機未安裝騰訊地圖APP"
val intent = Intent()
val destination = convertBaiduToGPS(toLatLng)
intent.setData(Uri.parse("qqmap://map/routeplan?type=walk&to=目的地&tocoord=${destination.latitude},${destination.longitude}&policy=1&referer=myapp"))
context?.startActivity(intent)
}
}
}catch(ex: ActivityNotFoundException){
Global.showToast(showToastTxt)
}
}
/**百度坐标系 (BD-09) 轉 火星坐标系(GCJ-02)的轉換*/
fun convertBaiduToGPS(latlng: LatLng) = CoordinateConverter().from(CoordinateConverter.CoordType.BD09LL).coord(latlng).convert()
在小米手機使用上面的 “判斷本地是否安裝第三方App” 的函數進行判斷,當重新安裝百度地圖APP後,發現該函數傳回false,是以改用try/catch的方式進行捕捉并判斷.
高德地圖、騰訊地圖的經緯度坐标類型都是GCJ02,百度的經緯度坐标類型則是BD09,由于上面函數中傳入的坐标是來源于百度SDK定位的,是以需要進行轉換,才能用于高德和騰訊的導航.
參考官方連結:http://lbsyun.baidu.com/index.php?title=androidsdk/guide/tool/coordinate