H5 高德地图获取当前位置信息

返回结果:jsonp_393330_({"status":"1","info":"OK","infocode":"10000","regeocode":{"formatted_address":"上海市松江区广富林街道谷阳北路2558号龙湖新壹城","addressComponent":{"country":"中国","province":"上海市","city":[],"citycode":"021","district":"松江区","adcode":"310117","township":"广富林街道","towncode":"310117006000","neighborhood":{"name":[],"type":[]},"building":{"name":[],"type":[]},"streetNumber":{"street":"谷阳北路","number":"2558号","location":"121.236969,31.0548569","direction":"西","distance":"74.036"},"businessAreas":[{"location":"121.20869520209061,31.04970863414635","name":"松江大学城","id":"310117"}]}}})

 

代码示例:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>浏览器定位</title>
    <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css" />
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.9&key=2386af58e9cd68f0b50dd68ab241490f"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
    <body>
        <div id='container' style="display:none;"></div>
        <!--<div id="tip"></div>-->
        <script type="text/javascript">
            /***************************************
            由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。
            ***************************************/
            var map, geolocation;
            //加载地图,调用浏览器定位服务
            map = new AMap.Map('container', {
                resizeEnable: true
            });
            map.plugin('AMap.Geolocation', function () {
                geolocation = new AMap.Geolocation({
                    enableHighAccuracy: true,//是否使用高精度定位,默认:true
                    timeout: 10000,          //超过10秒后停止定位,默认:无穷大
                    buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
                    zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
                    buttonPosition: 'RB'
                });
                map.addControl(geolocation);
                geolocation.getCurrentPosition();
                AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
                AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
            });
            //解析定位结果
            function onComplete(data) {
               alert(JSON.stringify(data))
                //var str = ['定位成功'];
                //str.push('经度:' + data.position.getLng());
                //str.push('纬度:' + data.position.getLat());
                //if (data.accuracy) {
                //    str.push('精度:' + data.accuracy + ' 米');
                //}//如为IP精确定位结果则没有精度信息
                //str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'));
                //document.getElementById('tip').innerHTML = str.join('<br>');
            }
            //解析定位错误信息
            function onError(data) {
                document.getElementById('tip').innerHTML = '定位失败';
            }
        </script>
    </body>
</html>

 

posted @ 2018-08-20 18:01  方金  阅读(2079)  评论(1编辑  收藏  举报