2017-06-22 75 views
1

IHAVE以下代码:给定一个GeoJSON文件,如何仅在Leaflet中显示特征质心?

$.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; 
    } 
    }); 
}); 

事实上,我想通过这种方法计算的质心通过的经纬度变量:turf.centroid(feature).geometry.coordinates

PointToLayer需要特征和经纬度作为参数,所以我怎样才能实现这?我尝试了很多东西,但没有奏效。

回答

1

首先,让我从API文档会开出L.GeoJson's pointToLayer option,重点煤矿:

pointToLayer 定义了如何以GeoJSON 卵传单层的功能。添加数据时,它在内部调用[...]

style A功能限定用于造型以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 , { ... }); 
}); 
相关问题