首页 > 技术文章 > js调用高德地图API获取地理信息进行定位

lyxzxp 2019-07-18 16:09 原文

<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>

 

推荐阅读