我有密歇根州所有县的GeoJSON文件。我已将此文件嵌入Leaflet地图中。
我希望每个县多边形在点击时弹出窗口中显示县名。这基本上是一个重复的of this question,除了我的地图有83个多边形,我想找到一种方法来做到这一点,而不连续调用L.bindPopup()
83次。
我应该查看某种javascript函数还是for循环?我对javascript一点都不熟悉,所以任何建议都会非常感激。
到目前为止我的代码:
var mymap = L.map('mapid').setView([43.293218, -84.607686], 7);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: ' OpenStreetMap' }).addTo(mymap);
function addDataToMap(data, map) {
var dataLayer = L.geoJson(data);
dataLayer.addTo(map);
}
$.getJSON("county_coords.geojson", function(data)
{
addDataToMap(data, mymap);
});
以下是我的geoJSON文件示例(county_coords.geojson)。也许有一种方法可以称之为" LABEL" geoJSON文件中的属性是否在弹出窗口中显示县名?
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"FIPSCODE": "001",
"FIPSNUM": 1,
"NAME": "Alcona",
"LABEL": "Alcona County",
"TYPE": "County",
"CNTY_CODE": "001",
"SQKM": 1798.557217,
"SQMILES": 694.423636,
"ACRES": 444428.3814,
"VER": "13a",
"LAYOUT": "landscape",
"PENINSULA": "lower",
"URL": "http://per510a.mtri.org:8090/download/attachments/327710/NC_001_Alcona_2005.zip?version=2&modificationDate=1182876202960&api=v2"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-83.886988854009,
44.540800273944
]...