我们在用手机查看网页时可以通过传入经纬度去设置目的地然后跳转到对应的地图导航软件,如果没有下载软件则会跳转到下载界面
不幸的是,由于这种行为受到移动操作系统和第三方应用的影响,可能没有一个放之四海而皆通的解决方案,这种行为可能会因不同的设备和应用版本而有所不同。当你在应用中实现深度链接时,测试和考虑你所瞄准的特定应用行为是很重要的。
<template>
<div class="app-container">
<div v-for="(location, index) in locations" :key="index">
<span>{{ location.name }}:</span>
<el-select
v-model="selectedType[index]"
clearable
@change="(e) => navigateToMap(location, selectedType[index])"
>
<el-option label="高德地图" value="gaode"></el-option>
<el-option label="腾讯地图" value="tencent"></el-option>
<el-option label="百度地图" value="baidu"></el-option>
</el-select
</div>
</div>
</template>
<script setup>
import { ref, toRefs, reactive } from "vue";
const data = reactive({
selectedType: [],
userLocation: {},
appOpenedFlag: false,
});
const { selectedType, userLocation, appOpenedFlag } = toRefs(data);
const locations = [
{
name: "南京热河路",
latitude: 32.088169,
longitude: 118.74247,
destination: "32.088169,118.74247",
},
{
name: "秦皇岛",
latitude: 39.9658,
longitude: 119.592224,
destination: "39.9658,119.592224",
},
{
name: "大理古城",
latitude: 25.69547,
longitude: 100.1655,
destination: "25.69547,100.1655",
},
];
// 获取用户当前位置
function getCurrentLocation() {
navigator.geolocation.getCurrentPosition((position) => {
userLocation.value = {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
});
}
// 生成导航链接
function navigateToMap(location, type) {
getCurrentLocation();
const { name, latitude, longitude, destination } = location;
let url = "";
switch (type) {
case "gaode":
url = `https://uri.amap.com/navigation?to=${longitude},${latitude},${name}&mode=car&policy=2&src=myLocation&coordinate=gaode&callnative=1`;
break;
case "tencent":
url = `qqmap://map/routeplan?type=drive&fromcoord=${userLocation.value.lat},${userLocation.value.lng}&tocoord=${destination}&referer=yourAppName`;
break;
case "baidu":
url = `baidumap://map/direction?origin=${userLocation.value.lat},${userLocation.value.lng}&destination=${destination}&mode=driving&src=yourAppName`;
break;
}
appOpenedFlag.value = false;
window.location.href = url;
// 检查用户在打开应用程序后是否返回到网页
setTimeout(() => {
if (!appOpenedFlag.value) {
redirectToDownloadPage(type);
}
}, 2000);
}
// 回调函数,用于在应用程序成功打开时设置标志
window.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
appOpenedFlag.value = true;
}
});
// 重定向到下载页面
function redirectToDownloadPage(type) {
switch (type) {
case "gaode":
window.location.href = "https://www.amap.com/";
break;
case "baidu":
window.location.href = "https://map.baidu.com/";
break;
case "tencent":
window.location.href = "https://map.qq.com/";
break;
}
}
</script>