[英]Show a marker for polygons from a GeoJSON file in Leaflet
我有一個傳單JS地圖,顯示GeoJSON文件中的數據。 然而,geojson中的一些特征是多邊形,有些是點。 我想用一個點替換每個多邊形(在質心中,在bbox的平均值中,無論如何,無關緊要,准確性並不重要),這樣我就可以“指出 - 整個” geojson文件,只顯示每個點的一個傳單標記,或者那個被轉換為點的多邊形。 我不想顯示多邊形的輪廓。
您可以使用L.GeoJSON
圖層的onEachFeature
選項,它需要為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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.