Android 打开第三方地图App进行导航

Posted 巨头之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 打开第三方地图App进行导航相关的知识,希望对你有一定的参考价值。

Ⅰ.前言

接到产品新需求,模仿高德地图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

以上是关于Android 打开第三方地图App进行导航的主要内容,如果未能解决你的问题,请参考以下文章

Android仿微信调用第三方地图应用导航(高德百度腾讯)

Android开发 PopupWindow弹窗调用第三方地图(百度,高德)实现导航功能

IOS实现应用内打开第三方地图app进行导航

Android 仿微信调用第三方应用导航(百度,高德腾讯)

iOS调用第三方地图App进行导航方法

打开 Apple 地图以设置目的地名称进行导航