读取Multipolygon geoJson数据并显示在Leaflet地图上

时间:2017-04-10 12:21:03

标签: javascript leaflet geojson polygons

我想在Leaflet地图上阅读/显示geoJson数据。我有10个多重多边形存储在geoJson文件中,并希望以不同的颜色为每个多边形着色。

阅读Leaflet' website上的文档,我使用以下代码来读取和着色多边形:

//Adding multipolygons to map
L.geoJSON(dataName, {
    style: function(feature) {
        switch (feature.properties.id) {
            case '100': return {color: "#ff0000"};
            case '200':   return {color: "#0ff000"};
        ...
        case '1000':   return {color: "#0000ff"};
        }
    }
}).addTo(map);

这是数据文件:

//Data file
var dataName = {"type": "FeatureCollection", "features":[

{ "type":"Feature","id":100,"properties":{"id":"100","count":0},"crs":{"type":"name","properties":{"name":"GEODATA"}},
"geometry":{"type":"MultiPolygon","coordinates":[MANY COORDINATES]
}},
{"type":"Feature","id":200,"properties":{"id":"200","count":0},"crs":{"type":"name","properties":{"name":"GEODATA"}},
"geometry":{"type":"MultiPolygon","coordinates":[MANY COORDINATES]
}},

...

{"type":"Feature","id":1000,"properties":{"id":"1000","count":0},"crs":{"type":"name","properties":{"name":"GEODATA"}},
"geometry":{"type":"MultiPolygon","coordinates":[MANY COORDINATES]
}]}

没有显示任何内容。我认为错误在于函数内部。不太确定"功能"确实。有线索吗?

THX!

1 个答案:

答案 0 :(得分:0)

看起来您应该使用dataName.features

L.geoJSON(dataName, {

应该是:

L.geoJSON(dataName.features, {

以下是我制作的another answer代码段:

geojson = L.geoJson(myGeoJson.features, { onEachFeature: onEachFeature, style: styleFeature, }).addTo(myLeafletMap);