通过外部百度地图 高德地图 腾讯地图和浏览器导航

338 阅读1分钟

##不废话直接上代码,主要实现时判断是否安装地图,有安装则去使用地图app导航,没有安装则使用浏览器打开地图导航

addItem("百度地图") {
    val uri =
        if (PackageUtils.isPackageInstalled(context, "com.baidu.BaiduMap")) {
            Uri.parse("baidumap://map/direction?destination=latlng:$latitude,$longitude|name:$buildingName&mode=driving")
        } else {
            Uri.parse("http://api.map.baidu.com/marker?location=$latitude,$longitude&title=$buildingName&content=$address&output=html")
        }
    runCatching {
        startActivity(Intent(Intent.ACTION_VIEW, uri))
    }.onFailure {
        ToastUtils.showShort("导航失败")
    }
    dismiss()
}
addItem("高德地图") {
    val uri =
        if (PackageUtils.isPackageInstalled(context, "com.autonavi.minimap")) {
            Uri.parse("amapuri://route/plan/?dlat=$latitude&dlon=$longitude&dname=$buildingName&dev=0&t=0")
        } else {
            Uri.parse("https://uri.amap.com/marker?position=$longitude,$latitude&name=$buildingName")
        }
    runCatching {
        startActivity(Intent(Intent.ACTION_VIEW, uri))
    }.onFailure {
        ToastUtils.showShort("导航失败")
    }
    dismiss()
}
addItem("腾讯地图") {
    val uri =
        if (PackageUtils.isPackageInstalled(context, "com.tencent.map")) {
            Uri.parse("qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=CurrentLocation&to=$buildingName&tocoord=$latitude,$longitude&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77")
        } else {
            Uri.parse("https://apis.map.qq.com/uri/v1/marker?marker=coord:$latitude,$longitude;addr:$address;title:$buildingName&coord_type=1&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77")
        }
    try {
        startActivity(Intent(Intent.ACTION_VIEW, uri))
    } catch (e: Exception) {
        ToastUtils.showShort("导航失败")
    }
    dismiss()
}

百度参数

g.jpg

高德参数

b.jpg