index.vue 3.56 KB
<template>
  <div class="map-box" :style="{ height: mapHeight + 'px' }">
    <div id="container" style="width: 100%; height: calc(100% - 30px)"></div>
    <div class="map-address">{{ address }}</div>
    <div id="myPageTop" style="position: absolute; top: 10px; width: 50%; left: 25%">
      <el-input placeholder="请输入关键字" v-model="input" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="searchAddress"></el-button>
      </el-input>
    </div>
  </div>
</template>

<script>
let map, marker, placeSearch
export default {
  name: 'map',
  props: {
    mapHeight: {
      type: Number,
      default: 300
    }
  },

  data() {
    return {
      input: '',
      address: '',
      lnglat: [],
      geocoderAddress: {}
    }
  },

  methods: {
    mapInit() {
      map = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 20
      })
      AMap.plugin(['AMap.PlaceSearch'], function() {
        //构造地点查询类
        placeSearch = new AMap.PlaceSearch({
          pageSize: 8, // 单页显示结果条数
          pageIndex: 1, // 页码
          citylimit: false, //是否强制限制在设置的城市内搜索
          // panel: "panel", // 结果列表将在此容器中进行展示。
          autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
          renderStyle: 'default'
        })
      })
      // 添加列表点选监听事件
      map.on('click', this.getAddress)
    },
    searchAddress() {
      let _this = this
      placeSearch.search(this.input, function(status, result) {
        if (status === 'complete' && result.poiList.pois.length) {
          let lnglat = result.poiList.pois[0].location
          _this.getAddress(lnglat)
        }
      })
    },
    addMarker(lnglat) {
      let position
      if (lnglat.lng) {
        position = [lnglat.lng, lnglat.lat]
      } else {
        position = lnglat
      }
      marker = new AMap.Marker({
        icon: new AMap.Icon({
          image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
          size: new AMap.Size(27, 34), //图标大小
          imageSize: new AMap.Size(27, 34)
        }),
        position: position,
        offset: new AMap.Pixel(-10, -10)
      })
      map.add(marker)
      map.setFitView()
    },
    getAddress(e) {
      let self = this
      let lnglat
      if (e.type == 'click') {
        lnglat = e.lnglat
      } else {
        let { lng, lat } = e
        lnglat = [lng, lat]
      }
      if (marker) {
        marker.setMap(null)
        marker = null
      }
      self.addMarker(lnglat)
      AMap.plugin('AMap.Geocoder', () => {
        const geocoder = new AMap.Geocoder()
        geocoder.getAddress(lnglat, (status, result) => {
          self.lnglat = lnglat
          self.address = result.regeocode.formattedAddress
          geocoder.getLocation(self.address, (status, result) => {
            if (status === 'complete' && result.info === 'OK') {
              this.geocoderAddress = result.geocodes[0]
            }
          })
        })
      })
    },
    selectAddress(e) {
      //这里获得点选地点的经纬度
      this.getAddress()
    }
  },

  mounted() {
    this.mapInit()
  }
}
</script>

<style scoped lang="scss">
.map-box {
  position: relative;
  width: 100%;
}
#container {
  position: absolute;
  height: 100%;
}
#panel {
  position: absolute;
  max-height: 90%;
  overflow-y: auto;
  top: 80px;
  right: 10px;
  width: 280px;
}
.map-address {
  position: absolute;
  line-height: 20px;
  bottom: 0;
}
</style>