vue百度地图的实现 根据经纬度回显地理位置

 效果图:

百度地图的实现:

 代码附上:

<template>
  <div style="height: auto">
    <baidu-map
      class="map"
      :center="center"
      :zoom="zoom"
      @ready="handler"
      :scroll-wheel-zoom="true"
      @click="clickEvent"
      ak="自己的ak!!!"
    >
      <bm-marker
        :position="{ lng: center.lng, lat: center.lat }"
        :dragging="true"
        animation="BMAP_ANIMATION_BOUNCE"
        @dragend="getClickInfo"
      >
      </bm-marker>
      <bm-control :offset="{ width: '10px', height: '10px' }">
        <bm-auto-complete v-model="keyword" :sugStyle="{ zIndex: 999999 }">
          <input
            type="text"
            placeholder="请输入搜索关键字"
            class="serachinput"
            v-model="addr"
          />
        </bm-auto-complete>
      </bm-control>
      <bm-local-search
        :keyword="keyword"
        :auto-viewport="true"
        style="width: 0px; height: 0px; overflow: hidden"
      ></bm-local-search>
      <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>

      <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>

      <bm-geolocation
        anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
        :showAddressBar="true"
        :autoLocation="true"
        @locationSuccess="getLoctionSuccess"
      ></bm-geolocation>

      <bm-view
        :style="{
          width: '100%',
          height: this.clientHeight + 'px',
          flex: 1,
          marginBottom: '-30px',
        }"
      ></bm-view>
    </baidu-map>
    <div class="footer">
      <Input class="lineinput" v-model.number="locData.longitude" disabled></Input>
      <Input class="lineinput" v-model.number="locData.latitude" disabled></Input>
      <Input class="lineinput" v-model="locData.address" disabled></Input>
    </div>
    <div class="drawer-footer">
      <Button type="text" @click.native="cancel">{{ $t("cancel") }}</Button>
      <Button type="primary" @click.native="findlocation">{{
        $t("sure")
      }}</Button>
    </div>
  </div>
</template>

<script>
import {
  BaiduMap,
  BmNavigation,
  BmView,
  BmGeolocation,
  BmCityList,
  BmControl,
  BmAutoComplete,
  BmLocalSearch,
  BmMarker,
} from "vue-baidu-map";
export default {
  name: "mapDialog",
  components: {
    BaiduMap,
    BmNavigation,
    BmView,
    BmGeolocation,
    BmCityList,
    BmControl,
    BmAutoComplete,
    BmLocalSearch,
    BmMarker,
  },
  data() {
    return {
      addr: "",
      keyword: "", // 通过BmLocalSearch搜素地址,然后再进行定位
      center: { lng: 121.833138, lat: 39.081725 },
      zoom: 12,
      mapVisible: true,
      locData: {
        longitude: "",
        latitude: "",
        address: "",
      },
      clientHeight: 300, // 屏幕高度
      // clientHeight:document.documentElement.clientHeight-90, // 屏幕高度
      iconUrl: "+",
    };
  },
  methods: {
    getClickInfo(e) {
      let that = this;
      this.center.lng = e.point.lng;
      this.center.lat = e.point.lat;
      var geoc = new BMap.Geocoder();
      geoc.getLocation(e.point, function (rs) {
        var addComp = rs.addressComponents;
        let addr =
          addComp.province +
          addComp.city +
          addComp.district +
          addComp.street +
          addComp.streetNumber;
        that.addres = addr;
        console.log("当前所在位置信息" + addr);
      });
    },
    handler({ BMap, map }) {
      let _this = this; // 设置一个临时变量指向vue实例,因为在百度地图回调里使用this,指向的不是vue实例;
      var geolocation = new BMap.Geolocation();
      geolocation.getCurrentPosition(
        function (r) {
          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;
    },
    //取消
    cancel() {
      this.locData.longitude = "";
      this.locData.latitude = "";
      this.locData.address = "";
      map.clearOverlays();
    },
    //点击地图监听
    clickEvent(e) {
      map.clearOverlays();
      let Icon_0 = new BMap.Icon(
        "http://api.map.baidu.com/img/markers.png",
         new BMap.Size(23, 25),
        {
          offset: new BMap.Size(10, 25), // 指定定位位置
          imageOffset: new BMap.Size(0, 0 - 10 * 25) // 设置图片偏移
        }
        // new BMap.Size(200, 200),
        // { anchor: new BMap.Size(18, 32), imageSize: new BMap.Size(36, 36) }
      );
      var myMarker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat), {
        icon: Icon_0,
      });
      map.addOverlay(myMarker);
      //用所定位的经纬度查找所在地省市街道等信息
      var point = new BMap.Point(e.point.lng, e.point.lat);
      var gc = new BMap.Geocoder();
      let _this = this;
      gc.getLocation(point, function (rs) {
        var addComp = rs.addressComponents;
        //console.log(rs.address);//地址信息
        _this.locData.address = rs.address;
      });
      this.locData.longitude = e.point.lng;
      this.locData.latitude = e.point.lat;
      this.keyword = "";
      this.addr = "";
    },
    //定位成功回调
    getLoctionSuccess(point, AddressComponent, marker) {
      map.clearOverlays();
      let Icon_0 = new BMap.Icon(
        "http://api.map.baidu.com/img/markers.png",
        new BMap.Size(23, 25),
        {
          offset: new BMap.Size(10, 25), // 指定定位位置
          imageOffset: new BMap.Size(0, 0 - 10 * 25) // 设置图片偏移
        }
        // new BMap.Size(200, 200),
        // { anchor: new BMap.Size(18, 32), imageSize: new BMap.Size(36, 36) }
      );
      var myMarker = new BMap.Marker(
        new BMap.Point(point.point.lng, point.point.lat),
        { icon: Icon_0 }
      );
      map.addOverlay(myMarker);
      this.locData.longitude = point.point.lng;
      this.locData.latitude = point.point.lat;
      // this.locData.latitude = point.point.lat;
    },
    findlocation() {
      this.$emit("findlocdata", this.locData);
      this.locData.longitude = "";
      this.locData.latitude = "";
      this.locData.address = "";
      map.clearOverlays();
    },
    mapShow() {},
  },
  mounted() {},
};
</script>
<style scoped lang="less">
.map {
  margin-left: 50px;
  width: 600px;
  position: relative;
  /* height: 300px; */
}
.footer {
  margin-top: 10px;
  text-align: right;
  margin-left: -8px;
  width: 658px;
}
.ivu-input-wrapper {
  display: inline-block;
  width: 91%;
  position: relative;
  vertical-align: middle;
  line-height: normal;
}
.drawer-footer {
  margin-top: 10px;
  text-align: right;
  position: relative;
  border: 0;
}
.serachinput {
  width: 300px;
  box-sizing: border-box;
  padding: 9px;
  border: 1px solid #dddee1;
  line-height: 20px;
  font-size: 16px;
  height: 38px;
  color: #333;
  position: relative;
  border-radius: 4px;
  -webkit-box-shadow: #666 0px 0px 10px;
  -moz-box-shadow: #666 0px 0px 10px;
  box-shadow: #666 0px 0px 10px;
}
.bm-view {
  /deep/ .BMap_cpyCtrl {
    display: none !important;
  }
}
</style>

根据经纬度回显地理位置:

代码附上:

<template>
  <baidu-map
    class="bm-view"
    ak="自己的ak!!!"
    :scroll-wheel-zoom="true"
    :center="center"
    :zoom="zoom"
    @ready="handler"
    ref="map"
  >
    <bm-marker
      :position="center"
      :dragging="true"
      animation="BMAP_ANIMATION_BOUNCE"
      :icon="{ url: icon, size: { width: 30, height: 30 } }"
    ></bm-marker>
    <bm-view
      :style="{
        width: '100%',
        height: this.clientHeight + 'px',
        flex: 1,
        marginBottom: '-30px',
      }"
    ></bm-view>
  </baidu-map>
</template>
<script>
import { BaiduMap, BmMarker, BmView } from "vue-baidu-map";
export default {
  name: "Map",
  components: { BaiduMap, BmMarker, BmView },
  props: {
    lng: {
      type: Number,
      default: "",
    },
    lat: {
      type: Number,
      default: "",
    },
    zoom: {
      type: Number,
      default: 16,
    },
  },
  data() {
    return {
      icon: require("../img/9c4bc8cd85d48ca7974f5e27fa2da37.png"),
      clientHeight: 300,
    };
  },
  computed: {
    center() {
      return {
        lng: this.lng,
        lat: this.lat,
      };
    },
  },
  methods: {
    handler({ BMap, map }) {
      console.error("MAP_ready", BMap, map);
      // let mapStyle= {style:'midnight'}
      // map.setMapStyle(mapStyle);
      this.zoom = this.zoom || 16;
    },
  },
};
</script>
<style lang="less" scoped>
.bm-view {
  width: 76%;
  height: 100%;
  margin-bottom: 40px;
  /*height: 300px;*/
  /deep/ .BMap_cpyCtrl {
    display: none !important;
  }
}
</style>

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现Vue中使用高德地图,并根据经纬度标记地理位置,可以按照以下步骤进行操作。 首先,需要在项目中引入高德地图的JavaScript SDK。可以通过在index.html文件中引入以下代码: ```html <script src="//webapi.amap.com/maps?v=1.4.15&key=your-amap-key"></script> ``` 其中,替换"your-amap-key"为你自己的高德地图API密钥。 接下来,在Vue组件中创建一个地图容器,可以使用div元素来承载地图。在组件的`mounted`生命周期钩子中,初始化地图并设置中心点和缩放级别: ```javascript mounted() { var map = new AMap.Map('map-container', { center: [经度, 纬度], zoom: 缩放级别 }); } ``` 其中,将`经度`和`纬度`替换为具体的地理位置经纬度坐标,并根据需要设置合适的`缩放级别`。 要在地图上标记一个地点,可以使用`AMap.Marker`对象。通过创建一个新的`AMap.Marker`实例,并设置其位置和图标,即可在地图上显示标记: ```javascript var marker = new AMap.Marker({ position: [经度, 纬度], icon: 'http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png' // 自定义标记图标 }); marker.setMap(map); // 将标记添加到地图上 ``` 同样,将`经度`和`纬度`替换为具体的地理位置经纬度坐标。如果需要自定义标记的图标,可以通过设置`icon`属性,将图标的URL传递给它。 最后,在模板中添加一个专门承载地图的div元素: ```html <div id="map-container"></div> ``` 这样,就可以在Vue实现高德地图的使用,并根据经纬度在地图上标记地理位置了。记得替换相关的经纬度和高德地图API密钥,以适应你的实际需求。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值