Mapbox fitBounds 自动调整地图视图

function flatten(input, output) {
    output = output || []
    var idx = output.length
    for (var i = 0; i < input.length; i++) {
        if (Array.isArray(input[i]) && Array.isArray(input[i][0])) {
            flatten(input[i], output)
            idx = output.length
        } else {
            output[idx++] = input[i]
        }
    }
    return Array.isArray(output[0]) ? output : [output]
}

function maxLat(coords) {
    return Math.max.apply(
        null,
        coords.map(function(d) {
            return d[1]
        }),
    )
}

function maxLng(coords) {
    return Math.max.apply(
        null,
        coords.map(function(d) {
            return d[0]
        }),
    )
}

function minLat(coords) {
    return Math.min.apply(
        null,
        coords.map(function(d) {
            return d[1]
        }),
    )
}

function minLng(coords) {
    return Math.min.apply(
        null,
        coords.map(function(d) {
            return d[0]
        }),
    )
}

function fetchEnvelope(coords) {
    var mmc = {
        minLng: minLng(coords),
        minLat: minLat(coords),
        maxLng: maxLng(coords),
        maxLat: maxLat(coords),
    }

    return {
        type: 'Feature',
        properties: {},
        geometry: {
            type: 'Polygon',
            coordinates: [
                [
                    [mmc.minLng, mmc.minLat],
                    [mmc.minLng, mmc.maxLat],
                    [mmc.maxLng, mmc.maxLat],
                    [mmc.maxLng, mmc.minLat],
                    [mmc.minLng, mmc.minLat],
                ],
            ],
        },
    }
}

function fetchExtent(coords) {
    return [minLng(coords), minLat(coords), maxLng(coords), maxLat(coords)]
}

// Adapted from http://stackoverflow.com/questions/2792443/finding-the-centroid-of-a-polygon
function fetchCentroid(vertices) {
    var centroid = {
        x: 0,
        y: 0,
    }

    var signedArea = 0
    var x0 = 0
    var y0 = 0
    var x1 = 0
    var y1 = 0
    var a = 0

    for (var i = 0; i < vertices.length - 1; i++) {
        x0 = vertices[i][0]
        y0 = vertices[i][1]
        x1 = vertices[i + 1][0]
        y1 = vertices[i + 1][1]
        a = x0 * y1 - x1 * y0

        signedArea += a
        centroid.x += (x0 + x1) * a
        centroid.y += (y0 + y1) * a
    }

    x0 = vertices[vertices.length - 1][0]
    y0 = vertices[vertices.length - 1][1]
    x1 = vertices[0][0]
    y1 = vertices[0][1]
    a = x0 * y1 - x1 * y0
    signedArea += a
    centroid.x += (x0 + x1) * a
    centroid.y += (y0 + y1) * a

    signedArea = signedArea * 0.5
    centroid.x = centroid.x / (6.0 * signedArea)
    centroid.y = centroid.y / (6.0 * signedArea)

    return [centroid.x, centroid.y]
}

function feature(obj) {
    return flatten(obj.geometry.coordinates)
}

function featureCollection(f) {
    return flatten(f.features.map(feature))
}

function geometryCollection(g) {
    return flatten(g.geometries.map(process))
}

function process(t) {
    if (!t) {
        return []
    }
    switch (t.type) {
        case 'Feature':
            return feature(t)
        case 'GeometryCollection':
            return geometryCollection(t)
        case 'FeatureCollection':
            return featureCollection(t)
        case 'Point':
        case 'LineString':
        case 'Polygon':
        case 'MultiPoint':
        case 'MultiPolygon':
        case 'MultiLineString':
            return flatten(t.coordinates)
        default:
            return []
    }
}

function envelope(t) {
    return fetchEnvelope(process(t))
}

function extent(t) {
    return fetchExtent(process(t))
}

function centroid(t) {
    return fetchCentroid(process(t))
}

function xMin(t) {
    return minLng(process(t))
}

function xMax(t) {
    return maxLng(process(t))
}

function yMin(t) {
    return minLat(process(t))
}

function yMax(t) {
    return maxLat(process(t))
}

export default {
    envelope,
    extent,
    centroid,
    xMin,
    xMax,
    yMin,
    yMax,
}

使用方法如下:使地图自动调整边界范围,以便完整展现地图要素

//先引用
import geojsonBounds from '@/utils/geojsonBounds'


function fitMapBounds() {
  let polygons = []
  routingContent.length &&
    routingContent.forEach((item, index) => {
      polygons.push({
        type: 'Feature',
        properties: {},
        geometry: {
          type: 'LineString',
          coordinates: item.coords
        }
      })
    })

  let bounds = geojsonBounds.extent({
    type: 'FeatureCollection',
    features: polygons
  })

  window.map &&
    window.map.fitBounds(bounds, {
      padding: {
        top: 6,
        left: 6,
        right: 6,
        bottom: 6
      }
    })
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值