<!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://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" /> <style> html,body,#container{ height:100%; } .info{ width:26rem; } </style> <body> <div id='container'></div> <div class="info"> <h4 id='status'></h4><hr> <p id='result'></p><hr> <p >因爲衆多瀏覽器已再也不支持非安全域的定位請求,爲保位成功率和精度,請升級您的站點到HTTPS。</p> </div> <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.13&key=您申請的key值"></script> <script type="text/javascript"> var map = new AMap.Map('container', { resizeEnable: true }); AMap.plugin('AMap.Geolocation', function() { var geolocation = new AMap.Geolocation({ enableHighAccuracy: true,//是否使用高精度定位,默認:true timeout: 10000, //超過10秒後中止定位,默認:5s buttonPosition:'RB', //定位按鈕的停靠位置 buttonOffset: new AMap.Pixel(10, 20),//定位按鈕與設置的停靠位置的偏移量,默認:Pixel(10, 20) zoomToAccuracy: true, //定位成功後是否自動調整地圖視野到定位點 }); map.addControl(geolocation); geolocation.getCurrentPosition(function(status,result){ if(status=='complete'){ onComplete(result) }else{ onError(result) } }); }); //解析定位結果 function onComplete(data) { document.getElementById('status').innerHTML='定位成功' var str = []; str.push('定位結果:' + data.position); str.push('定位類別:' + data.location_type); if(data.accuracy){ str.push('精度:' + data.accuracy + ' 米'); }//如爲IP精肯定位結果則沒有精度信息 str.push('是否通過偏移:' + (data.isConverted ? '是' : '否')); document.getElementById('result').innerHTML = str.join('<br>'); } //解析定位錯誤信息 function onError(data) { document.getElementById('status').innerHTML='定位失敗' document.getElementById('result').innerHTML = '失敗緣由排查信息:'+data.message; } </script> </body> </html>