mapbox 框选区域放大缩小地图

zoomInMap() {
      let map = this.mapBoxMap;
      let self = this;
      function coordToPolygon(coord1, coord2) {
        return {
          "type": "Feature", "geometry": {
            "type": "Polygon",
            "coordinates": [
              [
                [
                  coord1.lng,
                  coord1.lat
                ],
                [
                  coord1.lng,
                  coord2.lat
                ],
                [
                  coord2.lng,
                  coord2.lat
                ],
                [
                  coord2.lng,
                  coord1.lat
                ],
                [
                  coord1.lng,
                  coord1.lat
                ]
              ]
            ]
          }
        };
      }
      function updatelayer(coord1, coord2) {
        const _feature = coordToPolygon(coord1, coord2);
        if (map.getSource('polysource') != undefined) {
          map.getSource('polysource')
            .setData({
              'type': 'FeatureCollection',
              "features": [_feature]
            });
        }
      }
      if (self.mapClickType !== 2 && self.mapClickType !== 3) {
        map.off('mousedown', mousedown);
        map.off('mousemove', mousemove);
        map.off('mouseup', mouseup);
        return;
      }
      MapBoxService.removeMapLayerAndSource('polysource', map);
      map.addSource('polysource', {
        'type': 'geojson',
        'data': {
          'type': 'FeatureCollection',
          "features": []
        }
      });
      map.addLayer({
        'id': 'polysource',
        'type': 'fill',
        'source': 'polysource',
        'paint': {
          'fill-color': '#0080ff',
          'fill-opacity': 0.8,
          'fill-outline-color': '#fff'
        }
      });
      map.on('mousedown', mousedown);
      map.on('mousemove', mousemove);
      map.on('mouseup', mouseup);
      let point1;
      let point2;
      function mousedown(e) {
        if (self.mapClickType !== 2 && self.mapClickType !== 3) {
          return;
        }
        point1 = e.lngLat;
        point2 = e.lngLat;
        updatelayer(point1, point2);
        e.preventDefault();
      }
      function mousemove(e) {
        if (self.mapClickType !== 2 && self.mapClickType !== 3) {
          return;
        }
        if (point2) {
          point2 = e.lngLat;
          updatelayer(point1, point2);
        }
      }
      function mouseup(e) {
        if (self.mapClickType !== 2 && self.mapClickType !== 3) {
          return;
        }
        if (point2) {
          point2 = e.lngLat;
          updatelayer(point1, point2);
        }
        setTimeout(() => {
          let x = (point1.lng + point2.lng) / 2;
          let y = (point1.lat + point2.lat) / 2;
          map.easeTo({ center: [x, y] });
          setTimeout(() => {
            self.mapClickType === 2 && map.zoomIn();
            self.mapClickType === 3 && map.zoomOut();
          }, 300);
          map.off('mousedown', mousedown);
          map.off('mousemove', mousemove);
          map.off('mouseup', mouseup);
          self.zoomInMap();
        }, 100);
      }
    },

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Mapbox中的框选查找,可以按照以下步骤进行操作: 1. 首先,在Mapbox Studio中创建或选择一个合适的地图样式。可以使用Mapbox Studio Classic或Mapbox Studio进行样式的创建和编辑。 2. 在地图样式中,确保已经添加了所需的地图图层和数据源。这些图层可以是矢量图层、栅格图层或栅格切片图层,根据具体的需求进行选择。 3. 在Mapbox.js中,使用适当的代码库和API来实现框选查找功能。可以使用Mapbox GL JS、Mapbox.js、或者其他适用于你的项目的JavaScript库。 4. 使用合适的交互事件和工具,在地图上创建一个框选区域。例如,可以使用鼠标拖拽事件或触摸事件来创建一个矩形框选区域。 5. 在框选事件中,获取框选区域的坐标范围。可以使用Mapbox的API来获取框选区域的边界坐标。 6. 使用Mapbox的API或其他适用的方法,将框选区域的坐标范围传递给后端服务器或数据库进行查找操作。 7. 在后端服务器或数据库中,根据框选区域的坐标范围执行查询操作。可以使用Mapbox的Map Matching API来执行地理位置匹配操作,根据给定的坐标范围查找符合条件的地理位置点。 8. 将查询结果返回到前端,并在地图上展示查找到的地理位置点。可以使用Mapbox的API或其他适用的方法,在地图上标记或高亮显示查找到的地理位置点。 总结一下,实现Mapbox中的框选查找功能,需要在Mapbox Studio中创建地图样式,使用Mapbox.js或其他适用的JavaScript库来实现框选交互,将框选区域的坐标范围传递给后端进行查询操作,最后在地图上展示查询结果。具体的实现方式可以根据具体的需求和项目进行选择和调整。 Mapbox Studio样式作为Mapbox.js中的底图 Mapbox Studio手册 Mapbox API开发者页面

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值