Android 跳转到第三方地图App实现方案
Android 跳转到高德地图、腾讯地图、百度地图。
跳转到第三方地图目标地点定位、目标地点附近关键字搜索
高德地图:https://lbs.amap.com/api/amap-mobile/guide/android/route
腾讯地图:https://lbs.qq.com/uri_v1/guide-mobile-navAndRoute.html
百度地图:https://lbsyun.baidu.com/index.php?title=uri/api/android
/**
* Created by zGui on 2022/7/13.
* 跳转到第三方地图定位处理类
*/
class NavigationUtils {
companion object {
val JUMP_GAO_DE = "JUMP_GAO_DE"
val JUMP_TEN_XUN = "JUMP_TEN_XUN"
val JUMP_BAI_DU = "JUMP_BAI_DU"
private val PACKAGE_GAO_DE = "com.autonavi.minimap" //高德包名
private val PACKAGE_TEN_XUN = "com.tencent.map" //腾讯包名
private val PACKAGE_BAI_DU = "com.baidu.BaiduMap" //百度包名
private val TYPE_AROUND = false //true周边类型 false导航类型
/**
* 跳转地图App
* type 高德/腾讯/百度
* lat 目标纬度
* lon 目标经度
* poiname 目标名称
* keyWords TYPE_AROUND = true时,搜索目标附近的关键字
*/
@JvmStatic
fun jumpNavigation(context: Context, type: String, lat: String, lon: String, poiname: String) {
try {
val intent = Intent()
when (type) {
JUMP_GAO_DE -> {
if (isInstall(PACKAGE_GAO_DE)) {
if (TYPE_AROUND) {
val keyWords = "美食"
intent.data =
Uri.parse("androidamap://arroundpoi?sourceApplication=my&keywords=$keyWords&lat=$lat&lon=$lon&dev=0");
} else {
intent.data =
Uri.parse("androidamap://viewMap?sourceApplication=my&poiname=$poiname&lat=$lat&lon=$lon&dev=0")
}
context.startActivity(intent)
} else {
goToMarket(context, PACKAGE_GAO_DE)
}
}
JUMP_TEN_XUN -> {
if (isInstall(PACKAGE_TEN_XUN)) {
if (TYPE_AROUND) {
val keyWords = "篮球场"
intent.data =
Uri.parse("qqmap://map/nearby?coord:$lat,$lon;placeName=$keyWords&radius=5000&referer=myapp")
} else {
intent.data =
Uri.parse("qqmap://map/marker?marker=coord:$lat,$lon;title:$poiname;addr: &referer=myapp")
}
context.startActivity(intent)
} else {
goToMarket(context, PACKAGE_TEN_XUN)
}
}
JUMP_BAI_DU -> {
if (isInstall(PACKAGE_BAI_DU)) {
if (TYPE_AROUND) {
val keyWords = "公园"
intent.data = Uri.parse("baidumap://map/place/nearby?query=$keyWords¢er=$lat,$lon&radius=1000")
} else {
intent.data =
Uri.parse("baidumap://map/marker?location=$lat,$lon&title=$poiname&content=&traffic=on")
}
context.startActivity(intent)
} else {
goToMarket(context, PACKAGE_BAI_DU)
}
}
}
} catch (e: Exception) {
TLog.e("openNavigation: ", e.toString());
ToastUtils.showShort("请检查是否开启定位权限");
}
}
/**
* 判断是否安装应用
*/
private fun isInstall(packageName: String): Boolean {
/**
* targetSdkVersion >= 29(Android 11)会强制分区,
* 默认情况应用外部存储可见性将被过滤,应用需要对分区存储进行适配。
*/
return File("/data/data/$packageName").exists()
|| File("storage/emulated/0/Android/data/$packageName").exists()
}
/**
* 去APP市场下载
*/
private fun goToMarket(context: Context, packageName: String) {
try {
val uri: Uri = Uri.parse("market://details?id=$packageName")
val goToMarket = Intent(Intent.ACTION_VIEW, uri)
context.startActivity(goToMarket)
} catch (e: ActivityNotFoundException) {
}
}
}
}
此文章为个人开发时记录,有时时间有限,无法深入研究,若看到此文章后有其他见解或解决方式,欢迎留言交流
————————————————
版权声明:转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44158429/article/details/125780430