如何在Leaflet中将带权重的折线转换为多边形?

6

我需要允许用户使用指定的道路半径(通过"weight"参数可视化)绘制路径(使用折线)。

外观如下:

enter image description here

因此,我想知道如何在这条折线周围建立一个多边形并进行一些偏移? 就像这样:

enter image description here

2个回答

3

最终我使用JSTS库 (https://www.npmjs.com/package/jsts) 完成了它。

很简单:

//pathCoords should be an array of jsts.geom.Coordinate
var pathCoords = [];
var geometryFactory = new jsts.geom.GeometryFactory();

// on what distance new polygon should be built
var distance = (meters * 0.0001) / 111.12;
var shell = geometryFactory.createLineString(pathCoords);

// building a new polygon
var polygon = shell.buffer(distance);

// finally get your new polygon coordinates
var polygonCoords = polygon.getCoordinates();

我在我的Web应用程序中尝试了JSTS库,它完美地工作,但是我需要在Android上做同样的事情,但是我找不到类似的Android或Java库。 - AymanKun

2

像你所说的,你可以使用L.PolylinegetLatLngs方法来访问坐标,并使用它们来初始化L.Polygon。如果您需要访问在折线上设置的权重,可以使用其选项对象来实现:

var polyline = new L.Polyline([[25, -25], [25, 25], [-25, 25], [-25, -25]], {
    weight: 10,
}).addTo(map);

var polygon = new L.Polygon(polyline.getLatLngs(), {
    weight: polyline.options.weight
}).addTo(map);

如果你有更多需要复制的内容,甚至可以使用整个选项对象:

var polygon = new L.Polygon(polyline.getLatLngs(), polyline.options).addTo(map);

由于L.Polygon是从L.Polyline扩展而来的,因此这不会成为问题,因为它具有相同的选项。


2
也许我之前的问题表述不够清晰,我并不需要将折线的样式应用到多边形上,我需要在折线周围构建一个多边形。为了更好地理解,我附上了一张新图片。 - Ruslan Nigmatulin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接