定位 ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <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="http://cache.amap.com/lbs/static/main1119.css" /> <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.3&key=9f831272996d8fb9c2765393725e3f96"></script> <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script> <body> <div id='container'></div> <div id="tip"></div> <script type="text/javascript"> /*************************************** 由於Chrome、IOS10等已不再支持非安全域的瀏覽器定位請求,為保證定位成功率和精度,請儘快升級您的站點到HTTPS。 ***************************************/ var map, geolocation; map = new AMap.Map('container'); map.plugin('AMap.Geolocation', function() { geolocation = new AMap.Geolocation({ enableHighAccuracy: true, //是否使用高精度定位,預設:true timeout: 10000, //超過10秒後停止定位,預設:無窮大 maximumAge: 0, //定位結果緩存0毫秒,預設:0 convert: true, //自動偏移坐標,偏移後的坐標為高德坐標,預設:true showButton: true, //顯示定位按鈕,預設:true buttonPosition: 'LB', //定位按鈕停靠位置,預設:'LB',左下角 buttonOffset: new AMap.Pixel(10, 20), //定位按鈕與設置的停靠位置的偏移量,預設:Pixel(10, 20) showMarker: true, //定位成功後在定位到的位置顯示點標記,預設:true showCircle: true, //定位成功後用圓圈表示定位精度範圍,預設:true panToLocation: true, //定位成功後將定位到的位置作為地圖中心點,預設:true zoomToAccuracy: true //定位成功後調整地圖視野範圍使定位位置及精度範圍視野內可見,預設:false }); map.addControl(geolocation); geolocation.getCurrentPosition(); AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息 AMap.event.addListener(geolocation, 'error', onError); //返回定位出錯信息 }); var myplace = ''; //我的位置 var mycity = ''; //我所在的城市 //解析定位結果 function onComplete(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>'); // parent.loca(data); myplace = data.formattedAddress; //我的位置 mycity = data.addressComponent.city; //我所在的城市 console.log('str',data.position.getLng(),data.position.getLat()); console.log("我所在的地點為 " + data.formattedAddress); console.log("我所在的城市為 " + data.addressComponent.city); } function getSomeOfMyVal() { return myplace + "+" + mycity; } //解析定位錯誤信息 function onError(data) { document.getElementById('tip').innerHTML = '定位失敗'; } function getGeolocation() { geolocation.getCurrentPosition(); } </script> </body> </html>