Ror*_*ory 3 javascript json geojson web leaflet
我有一个传单JS地图,显示GeoJSON文件中的数据.然而,geojson中的一些特征是多边形,有些是点.我想用一个点替换每个多边形(在质心中,在bbox的平均值中,无论如何,无关紧要,准确性并不重要),这样我就可以"指出 - 整个" geojson文件,只显示每个点的一个传单标记,或者那个被转换为点的多边形.我不想显示多边形的轮廓.
iH8*_*iH8 12
您可以使用图层的onEachFeature选项L.GeoJSON,它会为您的featurecollection中的每个要素运行一个函数.在那里,您可以区分点和多边形.一个简单的例子:
var map = L.map('map', {
center: [0, 0],
zoom: 0
});
var geoJsonLayer = L.geoJson(featureCollection, {
onEachFeature: function (feature, layer) {
// Check if feature is a polygon
if (feature.geometry.type === 'Polygon') {
// Don't stroke and do opaque fill
layer.setStyle({
'weight': 0,
'fillOpacity': 0
});
// Get bounds of polygon
var bounds = layer.getBounds();
// Get center of bounds
var center = bounds.getCenter();
// Use center to put marker on map
var marker = L.marker(center).addTo(map);
}
}
}).addTo(map);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3674 次 |
| 最近记录: |