mapView.vue 4.8 KB
<template>
  <div class="map-box" :style="{ height: mapHeight + 'px' }">
    <div id="mapContainer" style="width: 100%; height: 100%;z-index: 999999"></div>
    <div class="map-address">{{ address }}</div>
    <div id="myPageTop" style="position: absolute; top: 10px; width: 50%; left: 25%">
      <el-input id="input" placeholder="请输入关键字" v-model="input" @keyup.enter.native="searchAddress" 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, geocoder
export default {
  name: 'map',
  props: {
    mapHeight: {
      type: Number,
      default: 300
    }
  },

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

  methods: {
    async mapInit(lng, lat) {
      const mapapi = this.$mapapi
      console.log(lng, lat, '==========', this.$mapapi)
      map = await mapapi.invoke('Map', 'mapContainer')
      if (lng) {
        this.init(mapapi, map, lng, lat)
        return true
      }
      mapapi.invoke('LocalCity', res => {
        console.log('IP', res)
        let { lng, lat } = res.center
        this.init(mapapi, map, lng, lat, false)
      })
    },
    init(mapapi, map, lng, lat, isMark = true) {
      console.log(lng, lat, 'lng, lat')
      mapapi.invoke('centerAndZoom', mapapi.invoke('Point', lng, lat), 15)
      map.enableScrollWheelZoom() // 启用滚轮放大缩小
      map.enableAutoResize() // 启用自动适应容器尺寸变化
      geocoder = mapapi.invoke('Geocoder')
      if (isMark) {
        this.addMarker([lng, lat], true)
      }
      // 右键菜单
      mapapi.invoke('mapAddEventListener', 'click', (e) => {
        console.log(e)
        this.getAddress(e.point)
      })
    },
    searchAddress() {
      const mapapi = this.$mapapi
      console.log(mapapi, 'mapapi')
      mapapi.invoke('getPoint', this.input, res => {
        console.log(this.input, res, 'getPoint')
        // this.addMarker(res)
        this.getAddress(res)
      }, '全国')
      // 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, isInit = false) {
      const mapapi = this.$mapapi
      let lnglatXY
      console.log(lnglat)
      if (lnglat.lng) {
        const { lng, lat } = lnglat
        lnglatXY = mapapi.invoke('Point', lng, lat)
      } else {
        lnglatXY = mapapi.invoke('Point', lnglat[0], lnglat[1])
      }
      if (marker) {
        mapapi.invoke('removeOverlay', marker)
      }
      marker = mapapi.invoke('Marker', lnglatXY, {
        icon: mapapi.invoke('Icon', require('@/assets/address.png'), mapapi.invoke('Size', 26, 38))
      })
      // if (!isInit) {
      //   mapapi.invoke('getLocation', lnglatXY, res => {
      //     console.log(res, 'getLocation')
      //     if (res) this.address = res.surroundingPois.length ? res.surroundingPois[0].address : res.address
      //   })
      // }
      mapapi.invoke('panTo', lnglatXY)
      mapapi.invoke('addOverlay', marker)
    },
    getAddress(e) {
      console.log(e)
      let self = this
      const mapapi = this.$mapapi
      let lnglat
      if (e.lng) {
        const { lng, lat } = e
        lnglat = mapapi.invoke('Point', lng, lat)
      } else {
        lnglat = mapapi.invoke('Point', e[0], e[1])
      }
      self.addMarker(lnglat)
      self.lnglat = lnglat
      geocoder.getLocation(lnglat, res => {
        console.log(res)
        this.address = res.surroundingPois.length ? res.surroundingPois[0].address : res.address
        this.geocoderAddress = res.content.address_detail
      })
      // 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()
    }
  },

  created() {
    // 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: -30px;
}
</style>