如何使用PolylineDecorator将箭头放在折线的每一行的末尾?

时间:2017-03-17 13:06:37

标签: javascript leaflet polyline-decorator

我正在尝试使用PolylineDecorator进行传单。我打算在每一行的末尾画一个带箭头的折线。我有这段代码:

var somePoints = [];
for (var rowIndex in rows) {
    somePoints.push(L.latLng(rows[rowIndex].Lat, rows[rowIndex].Lon));
}
var pl = L.polyline(somePoints);
pl.addTo(map);
var decorator = L.polylineDecorator(pl, {
    patterns: [
        // defines a pattern of 10px-wide dashes, repeated every 20px on the line
        {offset: 0, repeat: 50, symbol: L.Symbol.arrowHead({pixelSize: 8, polygon: false, pathOptions: {stroke: true}})}
    ]
}).addTo(map);

产生这个

enter image description here

我的意图是在每一行(标记位置)都有一个箭头。如果我将offset更改为"100%"而将repeat更改为0,那么我就有了:

enter image description here

我们在最后一个标记处有一个箭头,但没有其他标记的箭头。我知道我们可以通过将每一行绘制为offset "100%"repeat 0的单独折线来实现我想要的效果,但我想知道这是否可以实现使用decorator参数中包含两个以上点的单一折线。这是可能的,还是应该修改我的代码,使其具有带箭头的n-1折线?

1 个答案:

答案 0 :(得分:1)

我知道这就像个愚蠢的年纪,但实际上我正在为此目的构建插件:

https://github.com/slutske22/leaflet-arrowheads

根据注释中的请求,这是一个工作代码框,其中包含大量示例:https://codesandbox.io/s/leaflet-arrowheads-example-zfxxc

我几乎已经完成了自述文件的充实,并将很快要求将其添加到传单插件列表中。让我知道您的想法,如果您觉得有帮助/更多功能要求/一般反馈。