在 React 传单地图库中设置 GeoJSON 样式
Style GeoJSON in react leaflet map library
我已经在我的 React 项目中实现了 leaflet 地图库 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
每个省份颜色不同
您可以在 GeoJSON 包装器上使用 style
属性轻松实现这一点。创建一个接受特征作为参数的样式方法。然后在 fillColor 属性 中使用属性:{ electoralDistrict } 来标识选区和 return 所需的颜色:这是一个示例:
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>
);
}
}
我已经在我的 React 项目中实现了 leaflet 地图库 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
每个省份颜色不同
您可以在 GeoJSON 包装器上使用 style
属性轻松实现这一点。创建一个接受特征作为参数的样式方法。然后在 fillColor 属性 中使用属性:{ electoralDistrict } 来标识选区和 return 所需的颜色:这是一个示例:
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>
);
}
}