【问题标题】:Style GeoJSON in react leaflet map libraryreact 传单地图库中的样式 GeoJSON
【发布时间】:2020-03-23 09:19:42
【问题描述】:

我已经在我的 react 项目 https://react-leaflet.js.org/en/ 中实现了传单地图库,并实现了如下所示的 geojson 地图组件

class MapContainer extends React.Component {
  state = {
    greenIcon: {
      lat: 8.3114,
      lng: 80.4037
    },
    zoom: 8
  };

  grenIcon = L.icon({
    iconUrl: leafGreen,
    iconSize: [24, 24], // size of the icon
    //iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
    popupAnchor: [-3, -16]
  });

  render() {

    const positionGreenIcon = [
      this.state.greenIcon.lat,
      this.state.greenIcon.lng
    ];

    return (
      <div className="mapdata-container">
        <Map className="map" style={{height:'100%',width:'100%'}} center={positionGreenIcon} zoom={this.state.zoom}>
          <GeoJSON data={geo}/>
          <Marker position={positionGreenIcon} icon={this.grenIcon}>
            <Popup>I am a green leaf</Popup>
          </Marker>
        </Map>
      </div>
    );
  }
}

export default MapContainer;

看起来像这样

我想用不同的颜色为每个省份着色,但文档中没有很多关于如何做到这一点的内容。

这是我使用的 geojson 文件。 https://raw.githubusercontent.com/thejeshgn/srilanka/master/electoral_districts_map/LKA_electrol_districts.geojson

我如何fill每个省有不同的颜色。

【问题讨论】:

    标签: javascript reactjs leaflet geojson react-leaflet


    【解决方案1】:

    您可以使用 GeoJSON 包装器上的 style 属性轻松实现这一目标。创建一个接受特征作为参数的样式方法。然后在 fillColor 属性中使用属性:{electoralDistrict} 来识别区域并返回所需的颜色:这是一个示例:

    class MapContainer extends React.Component {
      state = {
        greenIcon: {
          lat: 8.3114,
          lng: 80.4037
        },
        zoom: 8
      };
    
      grenIcon = L.icon({
        iconSize: [24, 24], // size of the icon
        //iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
        popupAnchor: [-3, -16],
        iconUrl: leafGreen
      });
    
      giveColor = district => {
        switch (district) {
          case "Matara":
            return "red";
          case "Polonnaruwa":
            return "brown";
          case "Ampara":
            return "purple";
          default:
            return "white";
        }
      };
    
      style = feature => {
        const {
          properties: { electoralDistrict }
        } = feature;
        return {
          fillColor: this.giveColor(electoralDistrict),
          weight: 0.3,
          opacity: 1,
          color: "purple",
          dashArray: "3",
          fillOpacity: 0.5
        };
      };
    
      render() {
        const positionGreenIcon = [
          this.state.greenIcon.lat,
          this.state.greenIcon.lng
        ];
    
        return (
          <div className='mapdata-container'>
            <Map
              className='map'
              style={{ height: "100vh", width: "100%" }}
              center={positionGreenIcon}
              zoom={this.state.zoom}
            >
              <GeoJSON data={geo} style={this.style} />
              <Marker position={positionGreenIcon} icon={this.grenIcon}>
                <Popup>I am a green leaf</Popup>
              </Marker>
            </Map>
          </div>
        );
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2022-07-13
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多