代码之家  ›  专栏  ›  技术社区  ›  Paul Meems

无法让Resetstyle在NGX传单中工作

  •  0
  • Paul Meems  · 技术社区  · 7 年前

    我正在尝试用NGX传单在Angular6中做类似的事情,如下所述: https://leafletjs.com/examples/choropleth/

    我已经可以显示默认弹出窗口并更改样式 onmouseover 但不能得到 resetStyle 工作。

    我正在加载几个geojson数据集,并使用通用函数将它们作为单独的层添加。通过这些层,我想更改样式“onMouseOver”并将其重置为“onMouseOut”,单击该功能时,我想在页面右上角的一个分区中显示图表。此外,单击事件在我的代码中不起作用。

    从后端获取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和Style功能:

    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)
      });
    }
    

    任何帮助都将不胜感激。 将传单js.com中的示例转换为angular+ngx传单也可以帮助像我这样的新手。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Paul Meems    7 年前

    我自己找到了解决方案:

    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 .

    推荐文章