mapbox-gl开发:动态画线标绘

在mapbox-gl地图上,实现动态画线的效果,点击左键增加画线的点,鼠标移动,实现线随着鼠标移动位置的效果,点击右键结束画线。
mapbox-gl接口实现标绘数据存储在geojson数据中,通过添加数据源、修改数据源,实现地图上的标绘数据实时更新。

//添加geojson数据源:
      this.map.addSource(this.sourceid, {
            type: 'geojson',
           data: this.geojsondata
       })
//添加线标绘图层,设置线标绘的属性信息:
     this.map.addLayer({
       id: this.linelayerid,
       type: 'line',
       source: this.sourceid,
        layout: {
         'line-cap': 'round',
          'line-join': 'round'
       },
        paint: {
         'line-color': '#f00',
         'line-width': 2
       },
       filter: ['in', '$type', 'LineString']
    })

//鼠标动态移动事件,线的终点随着鼠标移动
this.mapmousemovefunc = function (e) {
    const _editfeature = scope.geojsondata.features[scope.getEditFeatureid()]
  _editfeature.geometry.coordinates.splice(_editfeature.geometry.coordinates.length - 1, 1, [e.lngLat.lng, e.lngLat.lat])
    scope.map.getSource(scope.sourceid).setData(scope.geojsondata)
  }
//鼠标点击事件,分为初次点击和之后点击的事件
  this.mapclickfunc = function (e) {
    if (_isfirst) {
//再次点击鼠标,修改最后一个点,并新增点
      const _editfeature = scope.geojsondata.features[scope.getEditFeatureid()]
      _editfeature.geometry.coordinates.splice(_editfeature.geometry.coordinates.length - 1, 1, [e.lngLat.lng, e.lngLat.lat])
      _editfeature.geometry.coordinates.push([e.lngLat.lng, e.lngLat.lat])
      let _centerpos = turf.centroid(scope.geojsondata) 
      _editfeature.properties.centerpos =  _centerpos.geometry.coordinates
      scope.map.getSource(scope.sourceid).setData(scope.geojsondata)
    } else {
   //初次点击鼠标,创建线数据
      _isfirst = true
      scope.editfeatureid = scope.createuuid()
      scope.geojsondata.features.push({
        type: 'Feature',
        geometry: {
          type: 'LineString',
          coordinates: [
            [e.lngLat.lng, e.lngLat.lat],
            [e.lngLat.lng + 0.000001, e.lngLat.lat]
          ]
        },
        properties: {
          id: scope.editfeatureid,
          sourceid: scope.sourceid,
          isdraw: true,
          centerpos: [e.lngLat.lng, e.lngLat.lat], 
          color: '#f00',
          opacity: 0.7
        }
      })
      scope.map.on('mousemove', scope.mapmousemovefunc)
    }
  }

//增加鼠标点击,右键结束标绘事件
this.map.on('click', this.mapclickfunc)
this.maprightclickfunc = function () {
    scope.stop()
  }
  this.map.on('contextmenu', this.maprightclickfunc)

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Mapbox GL Draw 是一款基于 Mapbox GL JS 的开源插件,用于在地图上进行绘制操作,支持点、线、面等多种几何要素的绘制和编辑,是一款非常适合用于态势标绘的工具。 使用 Mapbox GL Draw 进行绘制态势标绘,可以通过以下步骤实现: 1. 引入 Mapbox GL JS 和 Mapbox GL Draw 插件,可通过 npm 安装或手动下载引入。 2. 在页面中创建地图容器,并初始化地图: ```javascript var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', // 地图样式 center: [lng, lat], // 地图中心点经纬度 zoom: 13 // 地图缩放级别 }); ``` 3. 初始化 Mapbox GL Draw 插件,并将其添加到地图上: ```javascript var draw = new MapboxDraw({ displayControlsDefault: false, // 是否显示默认控件 controls: { // 自定义控件 point: true, line_string: true, polygon: true, trash: true } }); map.addControl(draw); ``` 4. 监听绘制事件,根据不同的绘制类型获取绘制的坐标信息: ```javascript map.on('draw.create', function(e) { var type = e.features[0].geometry.type; var coordinates = e.features[0].geometry.coordinates; // 根据绘制类型进行处理 }); ``` 5. 根据绘制类型,将绘制的要素添加到地图上: ```javascript map.addLayer({ 'id': 'drawn-feature', 'type': type, 'source': { 'type': 'geojson', 'data': { 'type': 'Feature', 'geometry': { 'type': type, 'coordinates': coordinates } } }, 'paint': { 'fill-color': '#f00', // 面颜色 'fill-opacity': 0.5, // 面透明度 'line-color': '#f00', // 线颜色 'line-width': 2 // 线宽度 } }); ``` 通过以上步骤,就可以使用 Mapbox GL Draw 进行绘制态势标绘了。除了绘制外,Mapbox GL Draw 还支持编辑、删除等操作,可以根据实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值