您的 GeoJSON 仍然缺少 id 属性,该属性对于您的 JSON 的 features 数组中的每个功能都必须是唯一的;本教程指出它需要位于特征对象的顶层,但在 properties 对象中包含 id 也可以,正如您在 codepen 中的另一张地图中所见。您的属性数据中存储在electoralDistrictCode 中的值非常适合id:
[{
"type": "Feature",
"id": "MON",
"properties": {
"electoralDistrictCode": "MON",
"electoralDistrict": "Monaragala",
// ...
},
"geometry": {/* ... */}
},{
"type": "Feature",
"id": "ANU",
"properties": {
"electoralDistrictCode": "ANU",
"electoralDistrict": "Anuradhapura",
// ...
},
"geometry": {/* ... */}
},
// ...
]
将其添加到 JSON 后,您的地图将在 AmCharts v4 中正确呈现。下面的演示,使用手动 AJAX 请求来改变 JSON 以使用库:
var map = am4core.create("chartdiv", am4maps.MapChart);
// indonesia geojson
fetch(
"https://raw.githubusercontent.com/GayanSandaruwan/elections/master/electoral_with_results.geojson"
)
.then(function(resp) {
if (resp.ok) {
return resp.json();
}
throw new Error("Unable to fetch json data");
})
.then(function(data) {
//add the missing id property before assigning it to the map
data.features.forEach(function (feature) {
feature.id = feature.properties.electoralDistrictCode;
});
map.geodata = data;
});
map.projection = new am4maps.projections.Miller();
var polygonSeries = new am4maps.MapPolygonSeries();
polygonSeries.useGeodata = true;
map.series.push(polygonSeries);
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/maps.js"></script>
<div id="chartdiv" style="width: 100%; height: 400px;"></div>