高德地圖使用官網 WEB端高德地圖使用javascript-apijavascript
高德地圖定位之瀏覽器定位源碼css
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> 7 <title>瀏覽器定位</title> 8 <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/> 9 <!-- <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key=您申請的key值"></script>--> 10 <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key=fd15ae6760592207f05cc78151bfe7b1"></script> 11 <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script> 12 <body> 13 <div id='container' class="gaode-map"></div> 14 <div id="tip"></div> 15 <script type="text/javascript"> 16 /*************************************** 17 因爲Chrome、IOS10等已再也不支持非安全域的瀏覽器定位請求,爲保證定位成功率和精度,請儘快升級您的站點到HTTPS。 18 ***************************************/ 19 var map, geolocation; 20 //加載地圖,調用瀏覽器定位服務 21 map = new AMap.Map('container', { 22 resizeEnable: true 23 }); 24 map.plugin('AMap.Geolocation', function() { 25 geolocation = new AMap.Geolocation({ 26 enableHighAccuracy: true,//是否使用高精度定位,默認:true 27 timeout: 10000, //超過10秒後中止定位,默認:無窮大 28 buttonOffset: new AMap.Pixel(10, 20),//定位按鈕與設置的停靠位置的偏移量,默認:Pixel(10, 20) 29 zoomToAccuracy: true, //定位成功後調整地圖視野範圍使定位位置及精度範圍視野內可見,默認:false 30 buttonPosition:'RB' 31 }); 32 map.addControl(geolocation); 33 geolocation.getCurrentPosition(); 34 AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息 35 AMap.event.addListener(geolocation, 'error', onError); //返回定位出錯信息 36 }); 37 //解析定位結果 38 function onComplete(data) { 39 var str=['定位成功']; 40 str.push('經度:' + data.position.getLng()); 41 str.push('緯度:' + data.position.getLat()); 42 if(data.accuracy){ 43 str.push('精度:' + data.accuracy + ' 米'); 44 }//如爲IP精肯定位結果則沒有精度信息 45 str.push('是否通過偏移:' + (data.isConverted ? '是' : '否')); 46 document.getElementById('tip').innerHTML = str.join('<br>'); 47 } 48 //解析定位錯誤信息 49 function onError(data) { 50 document.getElementById('tip').innerHTML = '定位失敗'; 51 } 52 </script> 53 </body> 54 </html>