欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

App 调用百度,高德地图实现路径规划

程序员文章站 2022-09-16 20:54:22
话不多说直接上代码open(address,title, lat, lon, mylat, mylon, myAddress) {// #ifdef APP-PLUSif (plus.os.name == "Android") {let hasBaiduMap = plus.runtime.isApplicationExist({pname: 'com.baidu.BaiduMap',action: 'baidumap://'});...

话不多说直接上代码

**可参考高德官网文档 https://lbs.amap.com/api/amap-mobile/guide/ios/route
注意: ios端调用高德地图的终点名称不要加 加上就不跳转了 具体是什么问题我还没研究清楚
**

open(address,title, lat, lon, mylat, mylon, myAddress) {
				// #ifdef APP-PLUS
				if (plus.os.name == "Android") {
					let hasBaiduMap = plus.runtime.isApplicationExist({
						pname: 'com.baidu.BaiduMap',
						action: 'baidumap://'
					});
					let hasAmap = plus.runtime.isApplicationExist({
						pname: 'com.autonavi.minimap',
						action: 'androidamap://'
					});
					// 此处获取自己的地址
					uni.getLocation({
					    type: 'wgs84',
						geocode: true,
					    success: (res) => {
					    	// 这里是自己的经纬度跟地址名存到data中
							this.mylat = res.latitude
							this.mylon = res.longitude
							this.myAddress = res.address.poiName
					    }
					}); 
					let urlBaiduMap =
						`bdapp://map/direction?destination=${address}&coord_type=bd09ll&mode=driving&src=andr.baidu.openAPIdemo`
					urlBaiduMap = encodeURI(urlBaiduMap)
					let urlAmap = `amapuri://route/plan/?sid=&slat=${mylat}&slon=${mylon}&sname=${myAddress}&did=&dlat=${lat}&dlon=${lon}&dname=${address}&dev=0&t=0`
					if (hasBaiduMap && !urlAmap) {
						plus.nativeUI.actionSheet({
							title: "选择地图应用",
							cancel: "取消",
							buttons: [{
								title: "百度地图"
							}]
						}, function(e) {
							switch (e.index) {
								case 1:
									plus.runtime.openURL(urlBaiduMap);
									break;
							}
						})
					} else if (hasBaiduMap && hasAmap) {
						plus.nativeUI.actionSheet({
							title: "选择地图应用",
							cancel: "取消",
							buttons: [{
								title: "百度地图"
							}, {
								title: "高德地图"
							}]
						}, function(e) {
							console.log(e.index)
							switch (e.index) {
								case 1:
									plus.runtime.openURL(urlBaiduMap);
									break;
								case 2:
									plus.runtime.openURL(urlAmap);
									break;
							}
						})
					} else {
						plus.nativeUI.alert("本机未安装指定的地图应用");
					}
				} else if (plus.os.name == "iOS") {
					plus.nativeUI.actionSheet({
						title: "选择地图应用",
						cancel: "取消",
						buttons: [{
							title: "百度地图"
						},{
							title: "高德地图"
						}]
					}, function(e) {
						if (e.index == 1) {
							let hasBaiduMap = plus.runtime.isApplicationExist({
								pname: 'com.baidu.BaiduMap',
								action: 'baidumap://'
							});
							// baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`
							let urlBaiduMap =
								`baidumap://map/marker?location=${lat},${lon}&title=${address}&content=${title}&src=ios.baidu.openAPIdemo&coord_type=gcj02`
							urlBaiduMap = encodeURI(urlBaiduMap)
							
							if (hasBaiduMap) {
								plus.runtime.openURL(urlBaiduMap)
							} else {
								plus.nativeUI.alert("本机未安装指定的地图应用");
							}
						} else if (e.index == 2) {
							let hasAmap = plus.runtime.isApplicationExist({
								pname: 'com.autonavi.minimap',
								action: 'iosamap://'
							});
							let urlAmap = `iosamap://path?sourceApplication=applicationName&sid=&slat=&slon=&sname=${myAddress}&did=&dlat=${lat}&dlon=${lon}&dname=&dev=0&t=0`
							// 检测是否安装高德地图
							if (hasAmap) {
								plus.runtime.openURL(urlAmap)
							} else {
								plus.nativeUI.alert("本机未安装指定的地图应用");
							}
						}
					})
				} 

自己百度了好久终于实现了,如果还有什么好的实现方法可以评论出来

本文地址:https://blog.csdn.net/weixin_45616872/article/details/107593848

相关标签: 调用地图