coordinateMap.vue 1.87 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
<template>
  <div class="coordinateMap">
    <div class="coordinateMap_input">
      <el-input
        v-model="lng"
        placeholder="点击地图或输入经度"
        @change="lnglatChange"
      ></el-input>
      <div style="width:50px"></div>
      <el-input
        v-model="lat"
        placeholder="点击地图或输入纬度"
        @change="lnglatChange"
      ></el-input>
    </div>
    <div
      id="map"
      class="map"
    >
    </div>
  </div>
</template>
<script src="https://webapi.amap.com/maps?v=1.4.15&key=2719fe261fee06a08dcb4980990879da"></script>
<script>
var map
var mouseTool
export default {
  data() {
    return {
      lastDot: '',
      marker: null,
      lng: '',
      lat: '',
    }
  },
  mounted() {
    this.initMap()
    //监听用户的点击事件
    map.on('click', (e) => {
      this.lng = e.lnglat.getLng()
      this.lat = e.lnglat.getLat()
      this.addDot()
    })
  },
  methods: {
    initMap() {
      map = new AMap.Map('map', {
        resizeEnable: true, //是否监控地图容器尺寸变化
        zoom: 11, //初始化地图层级
        center: [116.46,39.92] //初始化地图中心点
      });
    },
    lnglatChange() {
      this.addDot()
      //自适应中心点
      map.setFitView();
    },
    //增加点标记
    addDot(){
       if (this.marker) {
        this.marker.setMap(null);
        this.marker = null;
      }
      this.marker = new AMap.Marker({
        position: new AMap.LngLat(this.lng, this.lat)
      });
      let lnglat = {}
      lnglat.lng = Number(this.lng)
      lnglat.lat = Number(this.lat)
      this.$emit("giveLnglat", lnglat);
      map.add(this.marker);
    },
  }
}
</script>

<style lang="less" scoped>
.coordinateMap {
  width: 500px;
  .coordinateMap_input {
    display: flex;
    margin-bottom: 15px;
  }
  .map {
    width: 500px;
    height: 300px;
    border-radius:6px;
  }
}
</style>