使用 leaflet.js 在点周围添加设定大小的正方形多边形

问题描述 投票:0回答:2

有点奇怪,希望有人能帮忙。

在传单中,一旦用户输入纬度/经度并向地图添加一个点,我希望能够在该点周围添加一个 10 公里的正方形。

我尝试四处寻找计算方法来找到 x 公里外的正方形角点,但没有挖出任何东西。但肯定有更简单的方法!

有人有什么想法吗?如果只说 L.polygon 然后传入中心点和正方形大小,那就太好了。

谢谢,

泰勒

leaflet polygon
2个回答
9
投票

在您想要的纬度/经度上初始化一个

L.Circle
,半径为5000米,抓住边界并使用它们来初始化
L.Rectangle

new L.Rectangle(new L.Circle([0, 0], 5000).getBounds())

0
投票

这是使用传单 1.9 的示例


    const perimeter = 100_000 // we want a rectangle with a 100km perimeter
    const rectangleCenter = [13, -13] // center of the rectangle
    const circle = L.circle(rectangleCenter, { radius: perimeter / 8 }).addTo(map)
    const { _northEast, _southWest } = circle.getBounds()
    map.removeLayer(circle)
    L.rectangle([[_northEast.lat, _northEast.lng], [_southWest.lat, _southWest.lng]], { color: "#ff7800", weight: 1 }).addTo(map)
© www.soinside.com 2019 - 2024. All rights reserved.