Vue Baidu Map百度地图定位当前位置

<template>

  <div>

    <baidu-map

      class="map"

      @ready="handler"

      style="height: 500px;"

      :center="center"

      :zoom="15"

      :map-click="false"

      ak="ug2vkDRbX17m76RVbIjkTXBOK46OBnkg"

    >

      <div>

        <bm-marker

          :dragging="true"

          animation="BMAP_ANIMATION_BOUNCE"

          :position="center"

          @dragend="dragend"

        >213</bm-marker>

      </div>

    </baidu-map>

  </div>

</template>

 

<script>

import {

  BaiduMap,

  BmControl,

  BmView,

  BmAutoComplete,

  BmLocalSearch,

  BmMarker,

  BmGeolocation

} from "vue-baidu-map";

export default {

  components: {

    BaiduMap,

    BmControl,

    BmView,

    BmAutoComplete,

    BmLocalSearch,

    BmMarker,

    BmGeolocation

  },

  data() {

    return {

      center: {}

    };

  },

  methods: {

    handler: function({ BMap, map }) {

      map.enableScrollWheelZoom(true); //开启滚轮缩放

      // map.centerAndZoom('青岛市', 13)

      const hide = alert("正在获取当前省市请稍候..", 0);

      const _this = this;

      const geolocation = new BMap.Geolocation();

      geolocation.getCurrentPosition(

        function(r) {

          setTimeout(hide, 1000);

          console.log(r);

          _this.center = { lng: r.longitude, lat: r.latitude }; // 设置center属性值

          // _this.autoLocationPoint = { lng: r.longitude, lat: r.latitude }        // 自定义覆盖物

          _this.initLocation = true;

        },

        { enableHighAccuracy: true }

      );

 

      window.map = map;

      // 赋值,方便调用,本节被用到

      this.BMap = BMap;

      this.map = map;

    },

    // 拖动结束后

    dragend(e) {

      this.position = e.point;

      const _this = this;

      console.log(e.point);

      console.log(this.position);

      const gc = new this.BMap.Geocoder();

      gc.getLocation(e.point, function(rs) {

        console.log(rs);

        // var addComp = rs.addressComponents

        // this.addr = addComp.province + ', ' + addComp.city + ', ' + addComp.district + ', ' + addComp.street + ', ' + addComp.streetNumber

        _this.addr = rs.address;

      });

    }

  }

};

</script>

 

<style>

</style>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值