高德地图-----鼠标圈选获取标点信息

准备工作

  1. 在public中的index.html中初始化地图
<script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
  1. 在 .eslintrc.js 中配置AMap和AMapUI
globals: {
    "AMap": "true",
    "AMapUI":"true",
  }

实现思路

  1. 封装一个全局组件scottMap.vue,用于加载地图
  2. 封装一个基于scottMap.vue的组件mapDrawCircle.vue,用于在地图上通过鼠标画出圆
  3. 通过v-if和组件传参,确定需要的地图组件
  4. 组件嵌套封装代码(一个地图上面有多个功能),相比一个地图上封装一个功能,这种方式加载速度更快

scottMap.vue

<template>
  <div class="m-map">
    <div id="js-container" class="map">正在加载数据 ...</div>
    <div class="route">
        <map-draw-circle v-if="isDrowCircle" :map="map" :AMap="AMap"></map-draw-circle>
    </div>
  </div>
</template>

<script>
import MapDrawCircle from './scottMap/mapDrawCircle.vue'
/*
  传递参数:
    1. isDrowCircle:  是否需要画圆
*/
export default {
  name: 'mapIndex',
  components: { MapDrawCircle },
  props: {
    isDrowCircle: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      AMap: null,
      map: null,
      layer: '',
      mapConfig: {
        zoom: 10, // 设置地图显示的缩放级别
        center: [117.27, 31.86], // 设置地图中心点坐标
        layers: []
      }
    }
  },
  mounted () {
    this.initMap()
  },
  methods: {
    // 实例化地图
    initMap () {
      this.AMap = window.AMap
      this.map = new AMap.Map('js-container', this.mapConfig)
    }
  }

}
</script>

<style lang="css">
.m-map{ min-width: 500px; min-height: 300px; position: relative; }
.m-map .map{ width: 100%; height: 100%; }
.route{
    position: absolute;
    top: 10px;
    left: 200px;
    display: flex;
}
</style>

mapDrawCircle.vue

<template>
    <div>
        <el-button @click="doDrowCircle">是否画圆</el-button>
        <el-button @click="CancleDrowCircle">清空画圆</el-button>
    </div>
</template>

<script>
export default {
  name: 'mapDrawCirle',
  props: ['map', 'AMap'],
  components: {},
  data () {
    return {
      mouseTool: null,
      LngLatList: [
        {
          lng: 114.081505,
          lat: 22.576792,
          address: '1111',
          tel: 1111
        },
        {
          lng: 114.080175,
          lat: 22.576178,
          address: '22222',
          tel: 22222
        },
        {
          lng: 114.082874,
          lat: 22.579962,
          address: '3333',
          tel: 3333
        },
        {
          lng: 114.078604,
          lat: 22.579903,
          address: '4444',
          tel: 44444
        }
      ]
    }
  },
  methods: {
    doDrowCircle () {
      const that = this
      that.map.plugin(['AMap.MouseTool'], function () {
        that.mousetool = new AMap.MouseTool(that.map)
        that.mousetool.circle()
		// 鼠标工具绘制覆盖物结束时触发此事件,obj对象为绘制出来的覆盖物对象。
        that.mousetool.on('draw', function (e) {
          const circle = e.obj
          that.LngLatList.forEach(item => {
            const myLngLat = [item.lng, item.lat]
            // 判断标点是否在圈选的范围内
            if (circle.contains(myLngLat)) {
              console.log(item)
            }
          })
        })
      })
    },
    CancleDrowCircle () {
      this.mousetool.close(true)
    }
  }
}
</script>

<style lang='less' scoped>

</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uni-app的H5项目中,可以使用高德地图API来获取当前定位。下面是一种实现方式: 1. 首先,在uni-app项目中安装高德地图插件。可以通过以下命令进行安装: ``` npm install @types/amap-js-api --save ``` 2. 在需要获取定位的页面中,引入高德地图的JS API。可以在`index.html`文件中添加以下代码: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=your_amap_key"></script> ``` 其中,`your_amap_key`需要替换为你自己的高德地图开发者密钥。 3. 在页面的`methods`中,编写获取定位的方法。可以使用高德地图提供的`AMap.Geolocation`类来实现。以下是一个示例代码: ```javascript methods: { getLocation() { AMap.plugin('AMap.Geolocation', () => { const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为true timeout: 10000, // 超过10秒后停止定位,默认:无穷大 }); geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,result中包含经纬度等信息 const { lng, lat } = result.position; console.log('当前位置经纬度:', lng, lat); } else { // 定位失败 console.log('定位失败'); } }); }); } } ``` 4. 在页面中调用`getLocation`方法即可获取当前定位信息。可以在按钮的点击事件中调用该方法: ```html <button @click="getLocation">获取当前定位</button> ``` 这样,当用户点击按钮时,就会触发获取当前定位的操作,并将结果输出到控制台中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值