【无标题】

 

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值