给定一个GeoJSON文件,如何在传单中仅显示特征质心?



我有以下代码:

$.getJSON("rodents.geojson",function(data){
var rodents = L.geoJson(data,{
pointToLayer : function(feature,latlng){
var marker = L.marker(latlng);
//turf.centroid(feature).geometry.coordinates 
//  marker.bindPopup(feature.properties.Location + '<br/>' + feature.properties.OPEN_DT);
return marker;
}
});
});

实际上,我想通过此方法计算质心来传递 LatLng 变量turf.centroid(feature).geometry.coordinates

PointToLayer 将特征和 latlng 作为参数,那么我该如何实现呢?我尝试了很多方法,但没有奏效。

首先,让我引用 API 文档中L.GeoJsonpointToLayer选项,强调我的:

pointToLayer定义地理 JSON如何生成传单图层的函数。添加数据时在内部调用 [...]

style定义用于设置 GeoJSON线和多边形样式的路径选项的函数,在添加数据时在内部调用。[...]

如果您的 GeoJSON 不包含点,则不使用L.GeoJSONpointToLayer选项。

相反,我建议从您的GeoJSON创建一个质心的GeoJSON,例如:

$.getJSON("rodents.geojson", function(data){
centroids = {
type: 'FeatureCollection',
features: data.features.map(function(feat) {
return {
type: 'Feature',
properties: feat.properties,
geometry: turf.centroid(feat).geometry
}
})
};
//console.log(data);
//console.log(centroids);
var rodents = L.geoJson( centroids , { ... });
});

最新更新