LeafletJS 简单使用2 - 地图标点L.marker()、清除标点removeLayer、点击地图标点、清除地图map.remove()、画点线L.polygon()、画圆L.circle()

本文介绍了一个基于Leaflet的地图标注应用,实现了地图初始化、点击交互、坐标记录与确认等功能,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 业务需求

现有数据:初始经纬度信息 this.point
打开抽屉层:

  1. 创建地图
  2. 将当前初始经纬度信息使用标点方式通过图标展示出来
  3. 当前经纬度为地图中心点

点击地图:将初始标点进行清除 → 点击位置添加新标点 → 询问用户是否确定该标点位置

  1. 确定:关闭当前抽屉层,将最新点击的标点数据 this.clickPoint 传给父组件
  2. 取消:清除最新标点,展示初始标点

2. 代码示例

<!-- 地图标注弹出框 -->
<template>
  <section id="MarkPoint">
    <el-drawer
      title="地图标注"
      size="65%"
      :append-to-body="true"
      :before-close="beforeCloseHandler"
      @close="closeHandler"
      :visible.sync="innerDrawerVisible"
    >
      <!-- <cdt-map @result="mapInit"></cdt-map> -->
      <div id="map"></div>
    </el-drawer>
  </section>
</template>
<script>
// import CdtMap from '@/components/cdt-map/index'
import { mapGetters } from 'vuex'
import L from 'leaflet'
import 'leaflet/dist/leaflet.css'
import { tiledMapLayer } from '@supermap/iclient-leaflet'

export default {
  name: 'MapNew',
  components: {
    // CdtMap
  },
  data() {
    return {
      innerDrawerVisible: false,
      map: null,
      point: {}, // 经纬度初始值
      clickPoint: {}, // 点击地图 - 当前点击位置经纬度
      mapIcon: require('@/assets/mapIcon.png'), // 地图标点图标
      layer: null // 标点图层
    }
  },
  computed: {
    ...mapGetters(['sysConfigData'])
  },

  watch: {
    // 侦听点击地图时,当前标点经纬度发生变化
    clickPoint: {
      handler(val) {
        if (!!val.jd) {
          this.$confirm(
            `确定标注以下经纬度吗?<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;经度 ${val.jd}<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;纬度 ${val.wd}`,
            '提示',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              dangerouslyUseHTMLString: true
            }
          )
            .then((_) => {
              /**** 确定当前标点 ****/
              this.$message.success('标点成功')
              this.innerDrawerVisible = false
              this.$emit('updatePoint', val)
            })
            .catch((_) => {
              /**** 取消当前标点 ****/
              this.clickPoint = {}
              // 清除点击处标点
              this.map.removeLayer(this.layer)
              // 将标点标回原位置
              this.setPoint([this.point.wd, this.point.jd])
              this.$message.info('取消经纬度标注')
            })
        }
      }
    }
  },

  methods: {
    // 打开抽屉层
    open(pointer) {
      this.innerDrawerVisible = true
      this.point = pointer
      this.$nextTick(() => {
        this.mapInit()
      })
    },
    // 初始化地图
    mapInit() {
      this.map = L.map('map', {
        center: JSON.parse(this.sysConfigData['gx.arcgis.center']),
        maxZoom: this.sysConfigData['gx.arcgis.maxZoom'] * 1,
        zoom: this.sysConfigData['gx.arcgis.zoom'] * 1
      }).on('click', (ev) => {
        /**** 点击地图,清除原有坐标,将图标标点在当前点击位置 ****/
        this.clickPoint = {
          jd: ev.latlng.lng, // 经度
          wd: ev.latlng.lat // 纬度
        }
        this.map.removeLayer(this.layer) // 移除原有图标图层
        this.setPoint([ev.latlng.lat, ev.latlng.lng]) // 添加当前点击位置为新标点位置
      })

      tiledMapLayer(this.sysConfigData['gx.arcgis.mapUrl']).addTo(this.map) // 生成地图

      /**** 判断有没有经纬度,有的话标点 start ****/
      if (this.point.jd && this.point.wd) {
        // 画点线
        // // this.layer = L.polygon([JSON.parse(this.sysConfigData['gx.arcgis.center'])], {
        // this.layer = L.polygon([JSON.parse(this.sysConfigData['gx.arcgis.center']), [22, 108]], {
        //   color: 'blue',
        //   fillColor: 'none',
        //   opacity: 0.2
        // })
        // 画圆
        // this.layer = L.circle([22, 108], {
        //   color: 'green',
        //   fillColor: '#f03',
        //   fillOpacity: 0.5,
        //   radius: 10000
        // // }).addTo(this.map)
        // })
        // this.map.addLayer(this.layer)

        this.setPoint([this.point.wd, this.point.jd]) // 初始位置标点
        this.map.panTo(new L.LatLng(this.point.wd, this.point.jd)) // 当前图标位置为地图中心点
      }
      /**** 判断有没有经纬度,有的话标点 end ****/
    },

    // 标点
    setPoint(latlngArr) {
      /**
       * latlngArr数组:[纬度, 经度]
       */
      this.layer = new L.marker(latlngArr, {
        title: 'iconTitle',
        clickable: false,
        draggable: false,
        icon: L.icon({
          iconUrl: this.mapIcon, // 标点图标
          iconSize: [30, 36]
        })
      })
      this.map.addLayer(this.layer)
    },

    beforeCloseHandler(done) {
      this.$confirm('还有未保存的工作,确定关闭吗?')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    closeHandler() {
      this.layer = null
      if (this.map) this.map.remove() // 移除原有地图
      // this.map = null
    }
  }
}
</script>

<style lang='scss' scoped>
#map {
  position: absolute;
  top: 47px;
  bottom: 0;
  height: calc(100% - 47px);
  width: 100%;

  margin: 0 -30px;
  border: 2px solid red;
}
</style>

注意的点

  • 清除现有地图
    if (this.map) this.map.remove()
    
  • 移除指定标点图层
    this.map.removeLayer(this.layer)
    

补充示例:L.polygon() 画点线、L.circle() 画圆(该文档中未涉及)

  • 示例1 - 画点

    this.layer = L.polygon([JSON.parse(this.sysConfigData['gx.arcgis.center'])], {
      color: 'blue',
      fillColor: 'none',
      opacity: 0.2
    })
    this.map.addLayer(this.layer)
    

    在这里插入图片描述

  • 示例2 - 画线

    this.layer = L.polygon([JSON.parse(this.sysConfigData['gx.arcgis.center']), [22, 108]], {
      color: 'blue',
      fillColor: 'none',
      opacity: 0.2
    })
    this.map.addLayer(this.layer)
    

    在这里插入图片描述

  • 示例3 - 画圆

    this.layer = L.circle([22, 108], {
      color: 'green',
      fillColor: '#f03',
      fillOpacity: 0.5,
      radius: 10000
    }).addTo(this.map)
    // this.map.addLayer(this.layer) 
    /* L.circle().addTo(this.map) 或者 this.map.addLayer(this.layer)都可以将图层添加到地图 */
    

    在这里插入图片描述

3. 页面展示

  • 打开抽屉层页面展示
    在这里插入图片描述
  • 用户点击地图后,页面展示
    在这里插入图片描述
### Vue2 中实现高德地图 Marker 的添加与清除 在 Vue2 项目中集成高德地图 API 并实现 Marker 的添加与清除功能,可以通过以下方式完成。以下是详细的说明以及代码示例。 #### 初始化高德地图 首先需要通过 `@amap/amap-jsapi-loader` 加载高德地图 JSAPI,并初始化地图实例[^3]: ```javascript import AMapLoader from '@amap/amap-jsapi-loader'; export default { data() { return { map: null, markers: [], // 存储所有的 marker 实例 }; }, mounted() { this.initAMap(); }, methods: { initAMap() { AMapLoader.load({ key: 'your_amap_api_key', // 替换为你的高德地图 Key version: '2.0', plugins: [], }).then((AMap) => { this.map = new AMap.Map('container', { zoom: 12, // 设置缩放级别 center: [116.397428, 39.90923], // 设置中心点坐标 }); }); } } }; ``` #### 添加 Marker地图上 为了动态向地图添加 Marker,可以创建一个新的 Marker 对象并将它添加到地图中。同时将其存储在一个数组中以便后续管理[^1]: ```javascript addMarker(position) { const marker = new window.AMap.Marker({ position: position, // 地图上的位置 title: '新标记', }); marker.setMap(this.map); // 将 marker 绑定到地图 this.markers.push(marker); // 将 marker 存入数据模型中的 markers 数组 } ``` #### 清除所有 Markers 当需要移除所有已有的 Markers 时,可以从地图上销毁它们并清空保存的数组[^2]: ```javascript clearMarkers() { if (this.markers.length > 0) { this.markers.forEach((marker) => { marker.setMap(null); // 移除 marker }); this.markers = []; // 清空 markers 数据 } } ``` #### 完整组件示例 将上述逻辑组合成一个完整的 Vue2 组件: ```html <template> <div id="app"> <button @click="addRandomMarker">添加随机标记</button> <button @click="clearAllMarkers">清除所有标记</button> <div id="map-container"></div> </div> </template> <script> import AMapLoader from '@amap/amap-jsapi-loader'; export default { name: 'App', data() { return { map: null, markers: [], }; }, mounted() { this.initAMap(); }, methods: { async initAMap() { await AMapLoader.load({ key: 'your_amap_api_key', // 替换为你的高德地图 Key version: '2.0', plugins: [], }).then((AMap) => { this.map = new AMap.Map('map-container', { zoom: 12, center: [116.397428, 39.90923], }); }); }, addRandomMarker() { const randomLng = Math.random() * 0.05 + 116; // 随机经度范围 const randomLat = Math.random() * 0.05 + 39; // 随机纬度范围 const position = [randomLng, randomLat]; const marker = new window.AMap.Marker({ position: position, title: '随机标记', }); marker.setMap(this.map); this.markers.push(marker); }, clearAllMarkers() { if (this.markers.length > 0) { this.markers.forEach((marker) => { marker.setMap(null); }); this.markers = []; } }, }, }; </script> <style scoped> #map-container { width: 100%; height: 500px; } button { margin-right: 10px; } </style> ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值