Trying to get the [leafletLayers] option to work with ngx-leaflet.
I created a demo of this on Github, because Stackblitz is currently not loading leaflet correctly.
The demo works fine with the geo json data being added via the leaflet API after the mapReady call is made.
private createGeoJsonLayer(map: L.Map): L.GeoJSON<any> {
const layers: L.GeoJSON<any> = L.geoJSON(this.geoJsonData, {
style: {
weight: 2,
color: 'orange',
opacity: 0.8,
fillColor: 'red',
fillOpacity: 0.6,
},
onEachFeature: (feature, layer) => {
this.onEachFeature(feature, layer);
},
});
//==============================
// Add layers to map
//==============================
layers.addTo(map);
return layers;
}
However layers.addTo(map) is commented out, the geo json layer no longer renders.
I was expecting it to render anyways since it is assigned in the template like this:
<div
leaflet
style="height: 300px;"
leaflet
[leafletLayers]="geoJsonLayers"
[leafletOptions]="leafletOptions"
(leafletMapReady)="mapReady($event)"
></div>
Thoughts?
OK - Figured it out. The result of the call to
L.geoJSONhas to be wrapped in an array. So instead of returningL.geoJSONwe have to return[L.geoJSON].Here's a sample implementation:
Note that the
resetHightlightmethod needs access to the result, so the implementation should access the first item in array like this: