【WebGIS实例】(2)MapboxGL按边界定位:fitBounds

一、前言

开发工作中, 经常会有要求定位到某个面要素的操作。本篇便介绍一下Mapbox GL JS的fitBounds方法。

官方文档:fitbounds | Mapbox GL JS | Mapbox
官网示例:Fit a map to a bounding box

二、fitBounds 选项参考

fitBounds( bounds, options?, eventData? )

const bounds = [[-79, 43], [-73, 45]]; // 坐标格式参考:https://docs.mapbox.com/mapbox-gl-js/api/geography/#lnglatboundslike

map.fitBounds(bounds, {
	// easing:这个还没搞懂,官网实例参考:https://docs.mapbox.com/mapbox-gl-js/example/camera-animation/
	
	// 默认为false,决定了跳转的地图过渡动画
	linear: false, 
    // 地图视图跳转后允许的最大缩放级别
    maxZoom: 10, 
    // 给定的边界(bounds)的中心相对于地图中心的偏离距离,以像素为单位计量
    offset: [0,0],
   	// 按范围定位后地图容器的内边距
    padding: {top: 10, bottom:25, left: 15, right: 5},
    // 动画效果
    animate: true,
    // 动画的持续时间,以毫秒为单位
    duration: 2000,
});

三、实例

@turf/turf 版本:6.5.0
mapbox-gl 版本: 2.10.0
vue 版本:2.6.14

最简易的用法

map.fitBounds([
	[32.958984, -5.353521], // 边界的西南角
	[43.50585, 5.615985] // 边界的东北角
]);

获取点合集的边界范围并定位

数据格式:GeoJSON 格式的一堆点要素
const testJson =

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "coordinates": [
          116.14651904622104,
          25.64994276410873
        ],
        "type": "Point"
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "coordinates": [
          114.57822734231564,
          25.14633356787543
        ],
        "type": "Point"
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "coordinates": [
          111.21981499339012,
          27.808563561712532
        ],
        "type": "Point"
      }
    }
  ]
}

这部分使用的turf.js方法:envelope 获取多点的范围

const enveloped = this.turf.envelope(testJson);
map.fitBounds([[enveloped.bbox[0], enveloped.bbox[1]], [enveloped.bbox[2], enveloped.bbox[3]]],{
    linear: false,
    animate: true,
})

获取面要素的边界范围并定位

数据格式:GeoJSON 格式的面或多面
这部分使用的turf.js方法:bbox 获取边界

const bbox = this.turf.bbox(features);
this.map.fitBounds([[bbox[0], bbox[1]], [bbox[2], bbox[3]]],{
  linear: false,
  animate: true,
})

注意:

  1. 线要素同样适用
  2. 如果是多面的要素合集(FeatureCollection),那么可以用turf.center方法,获取合集中每一个多面的中心点再生成一个点要素合集,最后再依据这个点合集的边界范围并定位。

获取多个线要素的边界范围并定位

首先遍历所有的线要素(turf.geomEach),将每条线转换成点(turf.explode),然后把点合并到一个数组里。

for (let i = 0; i < features.length; i++) {
  this.turf.geomEach(features[i], function (currentSegment) {
	// 获取点位
	const explode = that.turf.explode(currentSegment);
	that.allPoint.push.apply(that.allPoint, explode.features)
  });
}

然后就可以通过turf.envelope获取点合集的边界范围

const enveloped = this.turf.envelope(this.turf.featureCollection(this.allPoint));
this.map.fitBounds([[enveloped.bbox[0], enveloped.bbox[1]], [enveloped.bbox[2], enveloped.bbox[3]]], {
  linear: false,
  animate: true,
})
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
fitBoundsMapbox GL JS库中的一个方法,用于根据给定的边界范围调整地图视图的缩放级别和中心位置。它可以确保指定的边界范围完全可见,并可以通过一些可选参数来自定义动画效果和其他行为。\[2\] 在你提供的引用中,fitBounds方法被用于将地图视图调整到指定的边界范围。具体来说,它使用了一个边界数组作为参数,该数组包含了边界的西南角和东北角的坐标。例如,\[\[32.958984, -5.353521\], \[43.50585, 5.615985\]\]表示了一个边界范围。\[1\] 除了边界数组之外,fitBounds方法还可以接受一些可选参数,用于自定义地图视图的过渡动画、最大缩放级别、偏移距离和内边距等。例如,可以设置animate参数为true来启用动画效果,设置duration参数来指定动画的持续时间。\[2\] 总之,fitBounds方法是一个方便的方法,可以根据给定的边界范围自动调整地图视图,使得指定的边界范围完全可见。它可以帮助用户快速定位和浏览感兴趣的区域。\[2\] #### 引用[.reference_title] - *1* *2* *3* [【WebGIS实例】(2)MapboxGL边界定位fitBounds](https://blog.csdn.net/ReBeX/article/details/127663092)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值