我正在尝试使用 ngx-leaflet 在 Angular6 中做类似的事情,如下所述: https ://leafletjs.com/examples/choropleth/
我已经可以显示默认弹出窗口并更改样式onmouseover
,但无法resetStyle
正常工作。
我正在加载几个 GeoJSON 数据集并使用通用函数将它们添加为单独的层。使用这些图层,我想更改样式“onmouseover”并将其重置为“onmouseout”,当单击该功能时,我想在页面右上角的 div 中显示图表。此外,单击事件在我的代码中也不起作用。
我从后端获取 GeoJSON 数据的通用函数:
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());
}
});
}
我的 onEachFeature 和样式功能:
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)
});
}
任何帮助将非常感激。将示例从 leafletjs.com 转换为 Angular+ngx-leaflet 也会帮助像我这样的新手。