简体   繁体   English

如何使用带有 GeoJSON featureCollection 的 LeafletJS polylineDecorator 插件?

[英]How to use the LeafletJS polylineDecorator plugin with a GeoJSON featureCollection?

I am trying to use the polylinedecorator leaflet plugin but can't seem to get it to work with my GeoJSON featureCollection and display on my map.我正在尝试使用 polylinedecorator 传单插件,但似乎无法让它与我的 GeoJSON featureCollection 一起使用并显示在我的地图上。

Here's my JSFIDDLE .这是我的JSFIDDLE Note: I pasted the polylinedecorator plugin code in the Javascript section.注意:我在 Javascript 部分粘贴了 polylinedecorator 插件代码。 Scroll to the bottom of the fiddle to see my actual code.滚动到小提琴底部以查看我的实际代码。

Here's my actual code:这是我的实际代码:

var data = {

  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            13.974609375,
            31.728167146023935
          ],
          [
            12.83203125,
            34.74161249883172
          ],
          [
            14.501953124999998,
            35.31736632923788
          ],
          [
            16.5234375,
            37.16031654673677
          ],
          [
            17.841796875,
            38.41055825094609
          ],
          [
            16.611328125,
            40.245991504199026
          ],
          [
            19.072265625,
            43.389081939117496
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            19.51171875,
            30.90222470517144
          ],
          [
            19.072265625,
            33.65120829920497
          ],
          [
            20.830078125,
            35.24561909420681
          ],
          [
            21.26953125,
            38.47939467327645
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            24.521484375,
            32.10118973232094
          ],
          [
            26.54296875,
            35.96022296929667
          ],
          [
            25.13671875,
            36.80928470205937
          ],
          [
            23.466796875,
            38.13455657705411
          ],
          [
            24.960937499999996,
            41.31082388091818
          ]
        ]
      }
    }
  ]
};

var polylines = L.geoJson(polylines, {
      onEachFeature: function (feature, layer) {
        L.polylineDecorator(layer, {
            patterns: [
                {offset: 25, repeat: 30, symbol: L.Symbol.arrowHead({pixelSize: 15, pathOptions: {fillOpacity: 1, weight: 0}})}
            ]
        })
    }
}).addTo(map);

You must remember that L.polylineDecorator(...) returns an instance of the decorator layers that you must add to the map , ie:您必须记住L.polylineDecorator(...)返回您必须添加到地图的装饰层实例,即:

var polylines = L.geoJson(json, {
  onEachFeature: function (feature, layer) {
    L.polylineDecorator(layer, {
      patterns: [
        {offset: 25, repeat: 30, symbol: L.Symbol.arrowHead({pixelSize: 15, pathOptions: {fillOpacity: 1, weight: 0}})}
      ]
    }).addTo(map);  /// Adds each decorator to the map!!!!
  } 
}).addTo(map);

Simply instantiating the polyline decorator does not add it to the map.简单地实例化折线装饰器不会将其添加到地图中。 You can, of course, store the decorators in a variable and add them later, or use a L.Control.Layers to toggle them on and off, etc.当然,您可以将装饰器存储在一个变量中并稍后添加它们,或者使用L.Control.Layers来打开和关闭它们,等等。

See it working at https://playground-leaflet.rhcloud.com/dey/1/edit?html,output看到它在https://playground-leaflet.rhcloud.com/dey/1/edit?html,output

That example adds the decorators directly to the map.该示例将装饰器直接添加到地图中。 If you want to add them to the L.GeoJSON instance instead, use addTo(this) .如果您想将它们添加到L.GeoJSON实例,请使用addTo(this) Or add them to a different L.LayerGroup and add the group to the map, etc etc etc.或者将它们添加到不同的L.LayerGroup并将组添加到地图等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM