function openAMap(lng, lat, address) {
var geolocation = new AMap.Geolocation();
geolocation.getCurrentPosition(function(status,result){
if(status=="complete"){
let web_url = `https://uri.amap.com/navigation?from=${result.position.lng},${result.position.lat},我的位置&to=${lng},${lat},${address}&mode=car&policy=1&src=com.mzwu.www&callnative=1`;
window.location.href = web_url;
}else{
alert("获取不到定位,请检查手机设置!");
}
});
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5唤起高德地图APP示例</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<!-- 高德地图安全密钥,必须在JSAPI脚本加载之前进行设置,否则设置无效。 -->
<script type="text/javascript">
window._AMapSecurityConfig = {
securityJsCode:'bf6265ecbc9e4ebff9c297369b9b106d',
}
</script>
<script src="https://webapi.amap.com/maps?v=2.0&key=f016c324446cdc91ea3297560d1d5925&plugin=AMap.Geolocation" type="text/javascript"></script>
<script src="https://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
//根据手机系统获取scheme
function getScheme(){
let agent = navigator.userAgent.toLowerCase();
if(agent.indexOf("iphone") != -1 || agent.indexOf("ipad") != -1){
return "iosamap://path";
}else{
return "amapuri://route/plan";
}
}
//唤起高德地图APP(注意url需要plugin参数)
function openAMap(lng, lat, address) {
let geolocation = new AMap.Geolocation();
geolocation.getCurrentPosition(function(status,result){
if(status=="complete"){
let app_url = `${getScheme()}?sourceApplication=com.mzwu.www&slat=${result.position.lat}&slon=${result.position.lng}&sname=我的位置&dlat=${lat}&dlon=${lng}&dname=${address}&dev=0&t=0`;
let web_url = `https://uri.amap.com/navigation?from=${result.position.lng},${result.position.lat},我的位置&to=${lng},${lat},${address}&mode=car&policy=1&src=com.mzwu.www&callnative=0`;
//尝试唤起高德地图App
window.location.href = app_url;
//唤起失败打开Web高德地图
var startTime = Date.now();
var count = 0;
var t = setInterval(function () {
if (++count < 30) {
return;
}
if (Date.now() - startTime > 800) {
clearInterval(t);
}
if (!(document.hidden || document.webkitHidden)) {
window.location.href = web_url;
}
}, 20);
window.onblur = function () {
clearInterval(t);
};
}else{
alert("获取不到定位,请检查手机设置!");
}
});
}
openAMap(120.149027,30.255584,'西湖断桥');
});
</script>
</body>
</html>