js調用高德地圖API獲取地理信息進行定位


<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=(需要自己去高德官網獲取)"></script>
<script type="text/javascript"> var map = new AMap.Map('mapContainer', { resizeEnable: true }) map.plugin('AMap.Geolocation', function() { var geolocation = new AMap.Geolocation({ // 是否使用高精度定位,默認:true enableHighAccuracy: true, // 設置定位超時時間,默認:無窮大 timeout: 10000, // 定位按鈕的停靠位置的偏移量,默認:Pixel(10, 20) buttonOffset: new AMap.Pixel(10, 20), // 定位成功后調整地圖視野范圍使定位位置及精度范圍視野內可見,默認:false zoomToAccuracy: true, // 定位按鈕的排放位置, RB表示右下 buttonPosition: 'RB' }) // 獲取當前位置信息 geolocation.getCurrentPosition(); // 監聽獲取位置信息成功的回調函數 AMap.event.addListener(geolocation, 'complete', onComplete); // 監聽獲取位置信息錯誤的回調函數 AMap.event.addListener(geolocation, 'error', onError); function onComplete (data) { // data是具體的定位信息 addComp = data.addressComponent; // 獲取信息中的省市區並放入到輸入框中 $("#fsenderAddress").val(addComp.province+"-"+addComp.city+"-"+addComp.district).trigger("change"); $("#fsenderCountyCode").val(addComp.adcode); let fsenderMsg = { 'fsenderAddress':$("#fsenderAddress").val(), 'fsenderCountyCode':$("#fsenderCountyCode").val() } window.sessionStorage.setItem('fsendMsg', JSON.stringify(fsenderMsg)); } function onError (error) { // 定位出錯 console.log(error) } }) </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM