vue项目使用高德地图定位当前地址

vue项目使用高德地图定位当前地址

一、在项目中安装vue-amap插件

  • 1.最终效果
    在这里插入图片描述
  • 2.安装命令
npm install vue-amap --save
  • 3.在main.js中引入
import VueAMap from 'vue-amap'
Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
  key: 'dd4cc5f831b67cde767de9a65f983650',
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor','AMap.Geolocation'],
  // 默认高德 sdk 版本为 1.4.4
  v: '1.4.4'
})

plugin字段表示插件引入,可以根据自己需求引入,但是想做当前定位,必须引入'AMap.Geolocation'

二、页面代码

<!-- 地图容器 -->
   <div id="map_container">
  <!-- 地图 -->
    <el-amap
        :center="center"
        :zoom="zoom"
        class="amap_demo"
        :plugin="plugin">
        <!-- 地图标记 -->
        <el-amap-marker v-for="(marker,index) in markers" :position="marker.position" :key="index"></el-amap-marker>
    </el-amap>
</div>
  • 1.amap_demo必须有高度:以下是css代码
#map_container{
      width: 100%;
      height: 210px;
}
.amap_demo{
       width: 100%;
       height: 100%;
 }

三、data数据

一看这么多不要害怕,很简单,实在不行cv就行

			zoom: 12,
            // 默认中心点
            center: [116.40,39.90],
            // 标记点
            markers: [
                // 标记点位置
                { position: [116.40,39.90]}
            ],
            // 当前地图的插件
            plugin: [{
              enableHighAccuracy: true,//是否使用高精度定位,默认:true
              timeout: 10000,          //超过10秒后停止定位,默认:无穷大
              maximumAge: 0,           //定位结果缓存0毫秒,默认:0
              convert: true,           //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
              showButton: true,        //显示定位按钮,默认:true
              buttonPosition: 'RB',    //定位按钮停靠位置,默认:'LB',左下角
              showMarker: false,        //定位成功后在定位到的位置显示点标记,默认:true
              showCircle: true,        //定位成功后用圆圈表示定位精度范围,默认:true
              panToLocation: true,     //定位成功后将定位到的位置作为地图中心点,默认:true
              zoomToAccuracy:true,//定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:f
              extensions:'all',
              pName: 'Geolocation',
              events: {
                init(o) {
                  // o 是高德地图定位插件实例
                  o.getCurrentPosition((status, result) => {
                    console.log(result)
                    if (result && result.position) {
                        // 将当前经纬度给中心点
                        that.center = [result.position.lng, result.position.lat];
                        // 将当前经纬度给标记点
                        that.markers[0].position = that.center;
                        that.loaded = true;
                        that.$nextTick();
                    }
                  });
                }
              }
            }],

在这里插入图片描述
打印信息
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43242112/article/details/107214148