简体   繁体   English

Leaflet.js-使用哪种方法从GeoJson添加标记

[英]Leaflet.js - Which method to be used for adding markers from GeoJson

I reffered official example provided by Leaflet.js. 我引用了Leaflet.js提供的官方示例 But things are not clear to me. 但是我不清楚。 And I am confuse about which one to use. 我对使用哪个感到困惑。

Currently, I am using pointToLayer for drawing markers : 目前,我正在使用pointToLayer绘制标记:

L.geoJson(mapGeoJsonData, {  pointToLayer: function (feature, latlng) { .... } })

But there is also another way to do the same thing using onEachFeature : 但是还有另一种使用onEachFeature进行相同操作的方法:

L.geoJson(mapGeoJsonData, {  onEachFeature: function (feature, layer) { .... } })

So which one to use when ? 那么什么时候使用哪个呢? What are pros and cons of pointToLayer and onEachFeature . pointToLayeronEachFeature的优缺点是什么?

Ref: https://github.com/Leaflet/Leaflet/blob/master/src/layer/GeoJSON.js 参考: https : //github.com/Leaflet/Leaflet/blob/master/src/layer/GeoJSON.js

pointToLayer is only called for Point and MultiPoint and expects you to return a Marker (you use this callback to create custom markers) 仅针对Point和MultiPoint调用pointToLayer,并且期望您返回Marker(您使用此回调创建自定义标记)

onEachFeature is called for all GeoJSON feature types (including Point and MultiPoint) and does not expect you to return anything (you use this callback to bind popups to Points, Lines ...) 所有GeoJSON要素类型(包括Point和MultiPoint)都将调用onEachFeature,并且不希望您返回任何内容(使用此回调将弹出窗口绑定到Points,Lines ...)

Note that you can't draw markers with onEachFeature but you can use pointToLayer to bind popup to Markers 请注意,您无法使用onEachFeature绘制标记,但可以使用pointToLayer将弹出窗口绑定到标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM