vue 高德地图 点击/搜索 获取坐标点信息

效果图

在这里插入图片描述
在这里插入图片描述

依赖
npm install vue-amap -S
mian.js文件
import AMap from 'vue-amap'
Vue.use(AMap)
AMap.initAMapApiLoader({
  key: '*******************', // 高德 key
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
  // 默认高德 sdk 版本为 1.4.4
  v: '1.4.4'
})
window._AMapSecurityConfig = {
  securityJsCode: '*******************' // 高德 securityJsCode
}
map.vue
<template>
    <div>
      <el-select
        class="tipInput"
        v-model="tipInput"
        filterable
        remote
        reserve-keyword
        clearable
        placeholder="输入地点进行搜索"
        :remote-method="remoteMethod"
        :loading="loading"
        @change="tipChange">
        <el-option
          v-for="item in tips"
          :key="item.id"
          :label="item.name"
          :value="item.id">
          <span style="float: left">{{ item.name }}</span>
          <span style="float: right; color: #8492a6; font-size: 13px">{{ item.district }}</span>
        </el-option>
        <template slot="prefix">
          <i class="el-icon-search el-input__icon"></i>
        </template>
      </el-select>
      <div id="container" style="width:100%;height:90vh" />
    </div>
</template>

<script>
// 绘制线路需要的坐标
export default {
  data () {
    return {
      tipInput: '', // 搜索框内容
      address: '', // 定位地址名称
      loading: false, // 远程搜索loading
      map: undefined, // map
      marker: undefined, // marker
      tips: [], // 查询到的地址
      coord: [] // 点坐标
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initMap()
    })
  },
  methods: {
    // 初始化地图
    initMap () {
      const mapObj = {resizeEnable: true, zoom: 10}
      this.map = new AMap.Map('container', mapObj)
      this.map.setDefaultCursor('default') // 设置鼠标指针样式
      this.map.on('click', this.onMapClick)
    },
    // 标记点
    onMarker () {
      if (this.marker) this.map.remove(this.marker)
      this.marker = new AMap.Marker({
        map: this.map,
        position: this.coord
      })
      this.marker.setLabel({
        direction: 'top',
        offset: new AMap.Pixel(10, 0), // 设置文本标注偏移量
        content: '<div>' + this.address + '</div>' // 设置文本标注内容
      })
    },
    // 鼠标点击地图
    onMapClick (e) {
      this.tipInput = ''
      this.coord = [e.lnglat.lng, e.lnglat.lat]
      this.getAddress()
    },
    // 查询
    remoteMethod (query) {
      if (query !== '') {
        this.loading = true
        const that = this
        this.map.plugin('AMap.Autocomplete', () => {
          // 实例化Autocomplete
          let autoOptions = {
            city: '全国'
          }
          let autoComplete = new AMap.Autocomplete(autoOptions)
          autoComplete.search(query, function (status, result) {
            that.tips = result.tips || []
            that.loading = false
          })
        })
      } else {
        this.tips = []
      }
    },
    // 选择搜索到的地址
    tipChange (value) {
      if (!value && this.marker) {
        this.$message.error('未获取到坐标信息,请选择其他地址')
        this.map.remove(this.marker)
        return
      }
      for (const tip of this.tips) {
        if (value === tip.id) {
          if (!tip.location) {
            this.$message.error('未获取到坐标信息,请选择其他地址')
            return
          }
          this.map.setZoom(16)
          this.map.setCenter(tip.location)
          this.coord = [tip.location.lng, tip.location.lat]
          this.getAddress()
          break
        }
      }
    },
    // 经纬度转化为详细地址
    getAddress () {
      let that = this
      this.map.plugin('AMap.Geocoder', function () {
        let geocoder = new AMap.Geocoder({
          radius: 100,
          extensions: 'all'
          // extensions: 'base'
        })
        geocoder.getAddress(that.coord, function (status, result) {
          if (status === 'complete' && result.info === 'OK') {
            let address = result.regeocode.formattedAddress
            that.address = address
            that.onMarker()
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tipInput {
  position: absolute;
  float: left;
  z-index: 999;
  left: 2%;
  top: 2%;
  width: 300px;
}
</style>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值