跳转到第三方地图

/*
	文件说明: openMap.js 
	 
	js文件是打开手机自带的地图, 默认的三个地图, 百度, 高德, 腾讯
	
	plus.runtime.isApplicationExist ------》 检测手机是否安装 地图软件
	
	openMapByAndroid(), openMapByIos  通过url方式调用第三方导航, 分为安卓, IOS
	
	点击开始导航, 软件下方会提示用户选择导航, 默认显示的是系统中安装的第三方导航,比如: 手机上安装的是高德, 只会提示高德地图,
	如果没有高德和百度, 会提示腾讯地图,腾讯地图是网页版的!
	
	首先判断系统是否安装地图软件, 没有安装会提示用户下载, 
	
	如果安装会自动调动第三方的导航软件, 进行导航
	
	
	使用说明:
	
	1. 在组件中 映入文件, 例如: import  Map from '文件的路径'
	
	2. 在组建中使用 MAP.openMap(lat, lng, name) 纬度, 经度, 名称
	
	3. openMap 是文件中暴露出去的方法
	
*/

let itemList = ["百度地图", "高德地图", "腾讯地图"];
// #ifdef APP-PLUS
var isBaidu = plus.runtime.isApplicationExist({
	pname: 'com.baidu.BaiduMap',
	action: 'baidumap://'
});
var isGaode = plus.runtime.isApplicationExist({
	pname: 'com.autonavi.minimap',
	action: 'iosamap://'
});
itemList = ["腾讯地图"];
if (isBaidu && isGaode) {
	itemList = ["百度地图", "高德地图"];
}
if (!isBaidu && isGaode) {
	itemList = ["高德地图"];
}
if (isBaidu && !isGaode) {
	itemList = ["百度地图"];
}
// #endif

function openMapByDefault(latitude, longitude, name) {
	uni.openLocation({
		latitude: latitude,
		longitude: longitude,
		name: name,
		address: "",
		fail: (err) => {
			console.log(err)
		},
	})
}

function openMapByAndroid(latitude, longitude, name) {
	let url = ''; // 回调地址
	let downloadUrl = ''; // 回调地址
	uni.showActionSheet({
		itemList: itemList,
		success: (res) => {
			var bdapp = "bdapp://map/direction?destination=name:" + name + "|latlng:" + latitude + "," + longitude +
				"&coord_type=bd09ll&src=hxbank";
			var bdappDown = "http://map.baidu.com/zt/qudao/newfengchao/1012337a/html/slide.html"
			var amapuri = "amapuri://route/plan/?sourceApplication=hxbank&dlat=" + latitude + "&dlon=" + longitude +
				"&dname=" + name + "&dev=0";
			var amapuriDown = "http://wap.amap.com/"
			// #ifdef APP-PLUS
			if (isBaidu && isGaode) {
				if (res.tapIndex == 0) {
					url = bdapp;
					downloadUrl = bdappDown
					openURL(url, downloadUrl)
				} else if (res.tapIndex == 1) {
					url = amapuri;
					downloadUrl = amapuriDown
					getGaoDeLocation(latitude, longitude, (location) => {
						url = "amapuri://route/plan/?sourceApplication=hxbank&dlat=" + location.lat + "&dlon=" + location.lng +
				"&dname=" + name + "&dev=0"
						openURL(url, downloadUrl)
					})
				}
			} else if (!isBaidu && isGaode) {
				if (res.tapIndex == 0) {
					// bMapTransqqMap(latitude,longitude)
					// var amapuri = "amapuri://route/plan/?sourceApplication=hxbank&dlat=" + obj.latitude + "&dlon=" + obj.longitude +
					// 	"&dname=" + name + "&dev=1";
					// var amapuriDown = "http://wap.amap.com/"
					url = amapuri;
					downloadUrl = amapuriDown
					getGaoDeLocation(latitude, longitude, (location) => {
							url = "amapuri://route/plan/?sourceApplication=hxbank&dlat=" + location.lat + "&dlon=" + location.lng +
					"&dname=" + name + "&dev=0"
							openURL(url, downloadUrl)
						})
				}
			} else if (isBaidu && !isGaode) {
				if (res.tapIndex == 0) {
					url = bdapp;
					downloadUrl = bdappDown
					openURL(url, downloadUrl)
				}
			} else {
				openMapByDefault(latitude, longitude, name)
			}
			// #endif
			// #ifndef APP-PLUS
			if (res.tapIndex == 0) {
				url = bdapp;
				downloadUrl = bdappDown
				openURL(url, downloadUrl)
			} else if (res.tapIndex == 1) {
				url = amapuri;
				downloadUrl = amapuriDown
				openURL(url, downloadUrl)
			} else {
				url = "qqmap://map/routeplan?type=drive&to=" + name + "&tocoord=" + latitude + "," + longitude + ""
				openURLTencent(url, latitude, longitude, name)
			}
			// #endif

		}
	})
}

function openMapByIos(latitude, longitude, name) {
	let url = ''; // 回调地址
	let downloadUrl = ''; // 回调地址
	uni.showActionSheet({
		itemList: itemList,
		success: (res) => {
			var bdapp = "baidumap://map/direction?destination=name:" + name + "|latlng:" + latitude + "," + longitude +
				"&coord_type=bd09ll&src=hxban";
			var bdappDown = "http://map.baidu.com/zt/qudao/newfengchao/1012337a/html/slide.html";
			var amapuri = "iosamap://route/plan/?sourceApplication=hxbank&dlat=" + latitude + "&dlon=" + longitude +
				"&dname=" + name + "&dev=0";
			var amapuriDown = "http://wap.amap.com/"
			// #ifdef APP-PLUS
			if (isBaidu && isGaode) {
				if (res.tapIndex == 0) {
					url = bdapp;
					downloadUrl = bdappDown
					openURL(url, downloadUrl)
				} else if (res.tapIndex == 1) {
					url = amapuri
					downloadUrl = amapuriDown
					getGaoDeLocation(latitude, longitude, (location) => {
						url = "iosamap://route/plan/?sourceApplication=hxbank&dlat=" + location.lat + "&dlon=" + location.lng +
							"&dname=" + name + "&dev=0";
						openURL(url, downloadUrl)
					})
				}
			} else if (!isBaidu && isGaode) {
				if (res.tapIndex == 0) {
					url = amapuri
					downloadUrl = amapuriDown
					getGaoDeLocation(latitude, longitude, (location) => {
						url = "iosamap://route/plan/?sourceApplication=hxbank&dlat=" + location.lat + "&dlon=" + location.lng +
							"&dname=" + name + "&dev=0";
						openURL(url, downloadUrl)
					})
				}
			} else if (isBaidu && !isGaode) {
				if (res.tapIndex == 0) {
					console.log(3)
					url = bdapp;
					downloadUrl = bdappDown
					openURL(url, downloadUrl)
				}
			} else {
				openMapByDefault(latitude, longitude, name)
			}
			// #endif
			// #ifndef APP-PLUS
			if (res.tapIndex == 0) {
				url = bdapp;
				downloadUrl = bdappDown
				openURL(url, downloadUrl)
			} else if (res.tapIndex == 1) {
				url = amapuri
				downloadUrl = amapuriDown
				openURL(url, downloadUrl)
			} else {
				url = "qqmap://map/routeplan?type=drive&to=" + name + "&tocoord=" + latitude + "," + longitude + ""
				openURLTencent(url, latitude, longitude, name)
			}
			// #endif

		}
	})
}

function getGaoDeLocation (lat, lng, success) {
	var X_PI = Math.PI * 3000.0 / 180.0;
	var x = lng- 0.0065, y = lat- 0.006;
	var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
	var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
	var bd_lng = z * Math.cos(theta);
	var bd_lat = z * Math.sin(theta);
	success({lng: bd_lng, lat: bd_lat})
}

function request (url, data, success) {
	var method = 'get'
	uni.request ({
		url: url,
		data,
		timeout: 3000,
		method,
		success: (res) => {
			console.log(res)
			if (res.statusCode === 200) {
				if (success) success(res.data)
			} else if (res.statusCode == 405) { 
				 uni.showToast({
					icon: 'none',
					title: '请求方法错误',
					duration: 1500
				})
			} else if (res.statusCode == 401) {
				uni.showToast({
					icon: 'none',
					title: '未登录或登录状态已超时',
					duration: 1500
				})
				setTimeout(() => {
					uni.reLaunch({
							url: '/pages/index/index.vue',
					})
				}, 1500)
			} else {
				uni.showToast({
					icon: 'none',
					title: '请求错误:' + res.statusCode,
					duration: 1500
				})
			}
		},
		fail: (res) => {
			uni.showToast({
				title: '网络请求失败',
				icon: 'none'
			})
			if (fail) fail(res)
		}
	})
}

function openURL(url, downLoadUrl) {
	url = encodeURI(url)
	// #ifdef APP-PLUS
	plus.runtime.openURL(url);
	// #endif
	// #ifndef APP-PLUS
	window.location.href = url;
	var startTime = Date.now();
	var count = 0;
	var endTime = 0;
	var t = setInterval(function() {
		count += 1;
		endTime = Date.now() - startTime;
		if (endTime > 800) {
			clearInterval(t);
		}
		if (count < 40) {
			return false;
		}
		if (!(document.hidden || document.webkitHidden)) {
			window.location.href = downLoadUrl;

		}
	}, 20);
	// #endif

}

function openURLTencent(url, latitude, longitude, name) {
	// #ifdef APP-PLUS
	plus.runtime.openURL(url);
	// #endif
	// #ifndef APP-PLUS
	window.location.href = url;
	// #endif
	var startTime = Date.now();
	var count = 0;
	var endTime = 0;
	var t = setInterval(function() {
		count += 1;
		endTime = Date.now() - startTime;
		if (endTime > 800) {
			clearInterval(t);
		}
		if (count < 40) {
			return false;
		}
		if (!(document.hidden || document.webkitHidden)) {
			openMapByDefault(latitude, longitude, name)
		}
	}, 20);
}
export default {
	/* 打开地图 */
	openMap(latitude, longitude, name) {
		// #ifdef MP-WEIXIN
		wx.openLocation({
			latitude: latitude,
			longitude: longitude,
			name: name,
			fail: (err) => {
				console.log(err)
			},
		})
		// #endif
		// #ifndef MP
		switch (uni.getSystemInfoSync().platform) {
			case 'android':
				console.log('运行Android上')
				openMapByAndroid(latitude, longitude, name)
				break;
			case 'ios':
				console.log('运行iOS上')
				openMapByIos(latitude, longitude, name, )
				break;
			default:
				openMapByDefault(latitude, longitude, name)
				console.log('运行在开发者工具上')
				break;
		}

		// #endif

	}
}

页面引用:

	...
	import MAP from '@/static/mapJs/openMap.js'
	...
	MAP.openMap(item.address.lat, item.address.lng, item.schoolName)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值