vue 高德地图点击获取坐标与地理位置

请添加图片描述

<template>
  <div class="coordinate">
    <div class="toolbar">
      <div>坐标: [ {{ lng }} , {{ lat }} ]</div>
      <div>地址: {{ address }}</div>
    </div>
    <div style="width: 100%;height: 300px;">
      <div id="coordinateMap" class="coordinateMap" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'MapDialog',
  props: {},
  data() {
    return {
      overlays: [],
      lng: '',
      lat: '',
      address: '',
      marker: '',
      geocoder: null,
      mouseTool: null
    }
  },
  created() {},
  mounted() {
    localStorage.clear()
    this.MapInit()
  },
  methods: {
    MapInit() {
      const that = this
      that.map = new AMap.Map('coordinateMap', {
        center: [113.274041, 23.141222],
        resizeEnable: true,
        expandZoomRange: true,
        zoom: 13,
        zooms: [3, 21]
      })
      that.drawBounds(that.map)
      // 异步同时加载多个插件
      AMap.plugin(['AMap.Scale'], function() {
        that.scale = new AMap.Scale()
        that.map.addControl(that.scale)
      })
      that.mouseTool = new AMap.MouseTool(that.map)
      // 监听draw事件可获取画好的覆盖物
      that.mouseTool.on('draw', function(e) {
        that.overlays.push(e.obj)
        that.mouseTool.close()
      })

      that.geocoder = new AMap.Geocoder({
        radius: 1000,
        extensions: 'all'
      })
      that.marker = new AMap.Marker()
      // 点击获取坐标
      const clickHandler = function(e) {
        that.lng = e.lnglat.getLng()
        that.lat = e.lnglat.getLat()
        const lnglat = [that.lng, that.lat]
        that.map.add(that.marker)
        that.marker.setPosition(lnglat)
        that.getAddress(that.lng, that.lat)
      }
      that.map.on('click', clickHandler)
    },
    // 获取地址
    getAddress(lng, lat) {
      const that = this
      that.geocoder.getAddress([lng, lat], function(status, result) {
        if (status === 'complete' && result.info === 'OK') {
          if (result && result.regeocode) {
            that.address = result.regeocode.formattedAddress
            that.$nextTick()
          }
        }
      })
    },
    // 限制地图区域
    drawBounds(map) {
      const opts = {
        subdistrict: 0,
        extensions: 'all',
        level: 'city'
      }
      const district = new AMap.DistrictSearch(opts)
      district.setLevel('广州市')
      district.search('广州市', (status, result) => {
        const outer = [
          new AMap.LngLat(-360, 90, true),
          new AMap.LngLat(-360, -90, true),
          new AMap.LngLat(360, -90, true),
          new AMap.LngLat(360, 90, true)
        ]
        const holes = result.districtList[0].boundaries

        const pathArray = [outer]
        pathArray.push.apply(pathArray, holes)
        const polygon = new AMap.Polygon({
          pathL: pathArray,
          strokeColor: '#001826',
          strokeWeight: 1,
          strokeOpacity: 0.5,
          fillColor: '#ffffff',
          fillOpacity: 1,
          strokeStyle: 'dashed',
          strokeDasharray: [10, 2, 10]
        })
        polygon.setPath(pathArray)
        map.add(polygon)
      })
    },
    // 限制地图区域END
    close() {
      this.mouseTool.close(true)
    },
  }
}
</script>

<style lang="scss" scoped>
.coordinate {
  width: 100%;
  height: 100%;
  .coordinateMap {
    width: 100%;
    height: 100%;
  }
}
</style>

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 高德地图周边附近是一个使用 Vue.js 框架来展示高德地图的功能。Vue.js 是一个流行的前端框架,能够帮助开发者构建用户界面。而高德地图则是国内领先的地图服务提供商,提供各种地图相关的功能和数据。 使用 Vue高德地图的组合,我们可以通过调用高德地图的 API 来获取地理位置信息,并在网页上显示出来。例如,可以使用高德地图JavaScript API 来获取周边地理位置的信息,然后在 Vue 组件中将这些信息展示出来。 在 Vue 组件中,我们可以使用 `mounted` 生命周期钩子函数来在组件加载完毕后调用高德地图的 API。通过调用 API 来获取当前用户所在位置的经纬度,并使用这些信息进行周边搜索。搜索结果可以是餐厅、购物中心等周边设施的信息。 一旦获取到了周边地理位置的信息,我们可以使用 Vue 的数据绑定机制将这些信息渲染到网页上,并展示在视图中。例如,可以使用 Vue 的列表渲染功能将获取到的周边位置逐一展示出来。 同时,我们还可以添加一些交互功能,如点击某个周边位置,可以展示更详细的信息窗口。这可以通过调用高德地图 API 的弹窗功能来实现,并结合 Vue 的事件监听机制来触发弹窗的显示。 总而言之,Vue 高德地图周边附近是一个结合了 Vue.js 框架和高德地图的功能,能够帮助开发者展示周边地理位置信息的网页应用。通过优雅的 Vue 组件和强大的高德地图 API,用户可以方便地浏览并了解周边设施的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值