0

我正在尝试使用 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 也会帮助像我这样的新手。

4

1 回答 1

1

我自己找到了解决方案:

this.mapService.getGeoJson(path, dataset).pipe(first()).subscribe(json => {
  // Create layer and add to map:
  const geoJsonLayer = L.Proj.geoJson(null, {
      onEachFeature: (feature, layer) => {
        // Create popup with all properties of the layer:
        let popupContent = '<h5>' + layerName + '</h5><p style="width: 100%; line-height: 1.5;">';
        for (const key in feature.properties) {
          popupContent += '<b style="min-width:25%; margin-right: 5px; display: block; float: left;">' + key + '</b>' +
            '<span>' + feature.properties[key] + '</span><br>';
        }
        popupContent += '</p>';
        layer.bindPopup(popupContent, {minWidth: 250, maxWidth: 400});
        layer.on('mouseover', (e) => this.highlightFeature(e));
        layer.on('mouseout', (e) => geoJsonLayer.resetStyle(e.target));
        layer.on('click', () => this.selectedFeature(feature));
      },
      attribution: layerName + ' &copy; CloudHydro'
    }
  ).addTo(this.map);

诀窍是不要使用单独的函数,onEachFeature而是创建一个内联函数。然后您可以访问geoJsonLayer所需的对象resetStyle

于 2018-10-12T07:03:37.803 回答