mapView.vue 5.73 KB
<template>
  <div class="map-box" :style="{ height: mapHeight + 'px', width: mapWidth + 'xp' }">
    <div id="container">
      <el-select v-model="input" class="map-input" filterable remote reserve-keyword placeholder="请输入" :remote-method="remoteMethod" @change="handleChangeAdr" :loading="loading">
        <el-option v-for="item in adrList" :key="item.id" :label="item.address" :value="item.id"></el-option>
      </el-select>
    </div>
    <div class="map-address">
      <img class="icon-map" src="./images/icon-map.png" />
      {{ address }}
    </div>
  </div>
</template>
<script>
let map, marker, placeSearch
export default {
  name: 'map',
  props: {
    mapHeight: {
      type: Number,
      default: 460
    },
    mapWidth: {
      type: Number,
      default: 740
    }
  },
  data() {
    return {
      input: [],
      address: '',
      adrList: [],
      lnglat: [],
      geocoderAddress: {}
    }
  },
  mounted() {
    this.mapInit()
  },
  methods: {
    // initMap() {
    //   console.log(document.getElementsByTagName('head'))
    //   var script = document.createElement('script')
    //   script.type = 'text/javascript'
    //   script.src = 'https://webapi.amap.com/loader.js'
    //   document.getElementsByTagName('head')[0].appendChild(script)
    // },
    mapInit() {
      /* eslint-disable */
      AMapLoader.load({
        /* eslint-enable */
        key: '44cc16e43ad7ed3358df507bd0618181', // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        // 需要使用的的插件列表
        plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.Geocoder'],
        AMapUI: {
          // 是否加载 AMapUI,缺省不加载
          version: '1.1', // AMapUI 版本
          plugins: ['overlay/SimpleMarker'] // 需要加载的 AMapUI ui插件
        },
        Loca: {
          // 是否加载 Loca, 缺省不加载
          version: '2.0' // Loca 版本
        }
      })
        .then(AMap => {
          map = new AMap.Map('container', {
            zoom: 20,
            autoFitView: true,
            resizeEnable: true,
            cursor: 'move'
          })
          if (this.lnglat.length) {
            this.addMarker(this.lnglat)
          }
          map.addControl(new AMap.Scale())
          map.addControl(new AMap.ToolBar())
          this.geocoder = new AMap.Geocoder()
          map.setDefaultCursor('pointer')
          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)
        })
        .catch(e => {
          console.error(e) //加载错误提示
        })
    },
    remoteMethod(query) {
      if (query == '') {
        return true
      }
      let _this = this
      placeSearch.search(query, function(status, result) {
        if (status === 'complete' && result.poiList.pois.length) {
          _this.adrList = result.poiList.pois
        }
      })
    },
    addMarker(lnglat) {
      let position
      if (lnglat.lng) {
        position = [lnglat.lng, lnglat.lat]
      } else {
        position = lnglat
      }
      this.lnglat = position
      marker = new AMap.Marker({
        map: map,
        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)
        }),
        // draggable: true,
        position: position,
        offset: new AMap.Pixel(-10, -10)
      })
      map.setCenter(position)
      map.add(marker)
      marker.setMap(map)
      // this.map.setFitView()
    },
    getAddress(e) {
      let self = this
      let position
      if (e.type == 'click') {
        position = e.lnglat
      } else {
        let { lng, lat } = e
        position = [lng, lat]
      }
      if (marker) {
        map.remove(marker)
        marker.setMap(null)
        marker = null
      }
      self.addMarker(position)
      AMap.plugin('AMap.Geocoder', () => {
        const geocoder = new AMap.Geocoder()
        geocoder.getAddress(position, (status, result) => {
          self.address = result.regeocode.formattedAddress
          geocoder.getLocation(self.address, (status, result) => {
            if (status === 'complete' && result.info === 'OK') {
              self.geocoderAddress = result.geocodes[0]
            }
          })
        })
      })
    },
    handleChangeAdr(val) {
      if (val) {
        var location = this.adrList.find(item => item.id == val)
        this.getAddress(location.location)
        this.input = ''
        this.adrList = []
      }
    }
  }
}
</script>
<style scoped lang="scss">
.map-box {
  display: flex;
  #container {
    position: relative;
    width: calc(100% - 284px - 16px);
    height: 100%;
  }
  .map-input {
    position: absolute;
    top: 32px;
    width: 60%;
    left: 25%;
    z-index: 2;
  }
}
#panel {
  position: absolute;
  max-height: 90%;
  overflow-y: auto;
  top: 80px;
  right: 10px;
  width: 280px;
}
.map-address {
  display: flex;
  box-sizing: border-box;
  width: 284px;
  height: 100%;
  background-color: #f9f9f9;
  padding: 20px;
  margin-left: 16px;
  line-height: 20px;
  .icon-map {
    width: 16px;
    height: 16px;
    margin-right: 6px;
  }
}
</style>