vue 使用高德地圖定位所在位置

1、獲取key
我們需要到[高德]開放平臺生成key

image.png

我這里選的是web端,提交后會生成一個key值

image.png
image.png

2、安裝vue-amap

       npm install vue-amap --save

3、在項目main.js引入vue-amap

import AMap from 'vue-amap';
Vue.use(AMap);

  // 初始化vue-amap
AMap.initAMapApiLoader({
  // 高德key
  key: '放入你的key',
  // 插件集合 (插件按需引入)
  plugin: ['AMap.Geolocation'] 
      });

3.使用

 <template>
 <div class="box">
    <div :style="{width:'100%',height:'300px'}">
      <el-amap vid="amap" :plugin="plugin" class="amap-demo" :center="center"></el-amap>
    </div>
</div>
</template>

<script>
export default {
data(){
  const self = this;
  return {
    center: [121.59996, 31.197646],
    lng: 0,
    lat: 0,
    loaded: false,
    plugin: [{
      enableHighAccuracy: true,//是否使用高精度定位,默認(rèn):true
      timeout: 100,          //超過10秒后停止定位,默認(rèn):無窮大
      maximumAge: 0,           //定位結(jié)果緩存0毫秒,默認(rèn):0
      convert: true,           //自動偏移坐標(biāo),偏移后的坐標(biāo)為高德坐標(biāo),默認(rèn):true
      showButton: true,        //顯示定位按鈕,默認(rèn):true
      buttonPosition: 'RB',    //定位按鈕停靠位置,默認(rèn):'LB',左下角
      showMarker: true,        //定位成功后在定位到的位置顯示點標(biāo)記,默認(rèn):true
      showCircle: true,        //定位成功后用圓圈表示定位精度范圍,默認(rèn):true
      panToLocation: true,     //定位成功后將定位到的位置作為地圖中心點,默認(rèn):true
      zoomToAccuracy:true,//定位成功后調(diào)整地圖視野范圍使定位位置及精度范圍視野內(nèi)可見,默認(rèn):f
      extensions:'all',
      pName: 'Geolocation',
      events: {
        init(o) {
          // o 是高德地圖定位插件實例
          o.getCurrentPosition((status, result) => {
            console.log(result)
            if (result && result.position) {
              self.lng = result.position.lng;
              self.lat = result.position.lat;
              self.center = [self.lng, self.lat];
              self.loaded = true;
              self.$nextTick();
            }
          });
        }
      }
    }]
  }
}
}
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容