I'm trying to do something similar in Angular6 with ngx-leaflet as described here: https://leafletjs.com/examples/choropleth/
I can already show the default pop-up and change the style onmouseover
, but can't get the resetStyle
to work.
I'm loading several GeoJSON-datasets and add them as separate layers using a generic function. With these layers I want to change the style `onmouseover' and reset it 'onmouseout' and when clicked on the feature I want to show a chart in a div on the top-right of my page. Also the click-event is not working in my code.
My generic function to get the GeoJSON data from my backend:
private loadGeoJsonDataSet(path: string, dataset: string, geometryType: string, layerName: string, fitBounds: boolean): void {
this.mapService.getGeoJson(path, dataset).pipe(first()).subscribe(json => {
// Create layer and add to map:
const geoJsonLayer = L.Proj.geoJson(null, {
// onEachFeature: (feature, layer) => {
// layer.bindPopup('<h5> Test:' + feature.properties.gid + '</h5>');
// },
onEachFeature: this.onEachFeature.bind(this),
attribution: 'CloudHydro'
}
).addTo(this.map);
// Set options:
switch (geometryType) {
case 'point': {
geoJsonLayer.options.style = this.getPointStyle;
geoJsonLayer.options.pointToLayer = (feature, latlng) => {
return L.circleMarker(latlng, this.circleMarkerOptions);
};
break;
}
case 'polyline': {
geoJsonLayer.options.style = this.getPolylineStyle;
break;
}
case 'polygon': {
geoJsonLayer.options.style = this.getPolygonStyle;
break;
}
default: {
geoJsonLayer.options.style = this.getPolygonStyle;
break;
}
}
// Add data to the layer:
geoJsonLayer.addData(json);
// Add layer to the layer control:
this.layersControl.overlays[layerName] = geoJsonLayer;
if (fitBounds) {
this.map.flyToBounds(geoJsonLayer.getBounds());
this.map.fitBounds(geoJsonLayer.getBounds());
}
});
}
My onEachFeature and style functions:
private highlightFeature(e) {
const layer = e.target;
layer.setStyle({
weight: 3, color: '#333',
});
if (!L.Browser.ie && !L.Browser.opera12) {
layer.bringToFront();
}
}
private resetHighlight(e) {
const layer = e.target;
--> Not working: layer.resetStyle(layer);
}
private clickedOnFeature(feature, layer) {
--> Not working: console.log('In clickedOnFeature', feature);
}
private onEachFeature(feature, layer) {
layer.bindPopup('<h5> GID:' + feature.properties.gid + '</h5>');
layer.on({
mouseover: this.highlightFeature,
mouseout: this.resetHighlight,
click: this.clickedOnFeature(feature, layer)
});
}
Any help would be really appreciated. Converting the examples from leafletjs.com to Angular+ngx-leaflet would also help novices like me.