我推荐了official example由 Leaflet.js 提供。但事情对我来说并不清楚。我很困惑该使用哪一个。
目前,我使用pointToLayer来绘制标记:
L.geoJson(mapGeoJsonData, { pointToLayer: function (feature, latlng) { .... } })
但是还有另一种方法可以使用 onEachFeature 完成相同的操作:
L.geoJson(mapGeoJsonData, { onEachFeature: function (feature, layer) { .... } })
那么什么时候使用哪一个呢? pointToLayer 和 onEachFeature 的优缺点是什么。
最佳答案
引用号:https://github.com/Leaflet/Leaflet/blob/master/src/layer/GeoJSON.js
pointToLayer 仅针对 Point 和 MultiPoint 调用,并期望您返回一个标记(您使用此回调来创建自定义标记)
onEachFeature 会针对所有 GeoJSON 要素类型(包括 Point 和 MultiPoint)调用,并且不期望您返回任何内容(您使用此回调将弹出窗口绑定(bind)到 Points、Lines ...)
请注意,您无法使用 onEachFeature 绘制标记,但可以使用 pointToLayer 将弹出窗口绑定(bind)到标记
关于javascript - Leaflet.js - 使用哪种方法从 GeoJson 添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24343341/