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

时间:2016-03-21 13:44:10

标签: routes geometry leaflet polyline

我需要允许用户绘制具有指定道路半径的路线(使用折线)(视觉上用“重量”参数完成)。

在视觉上它看起来像是:

enter image description here

所以我想知道如何在这条折线周围建立一个带有一些偏移的多边形?像这样:

enter image description here

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:2)

如您所说,您可以使用getLatLngs L.Polyline方法访问坐标并使用它们初始化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.PolygonL.Polyline扩展而不会成为问题,因为它具有相同的选项。