Reputation: 2454
I reffered official example provided by 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 :
L.geoJson(mapGeoJsonData, { pointToLayer: function (feature, latlng) { .... } })
But there is also another way to do the same thing using onEachFeature:
L.geoJson(mapGeoJsonData, { onEachFeature: function (feature, layer) { .... } })
So which one to use when ? What are pros and cons of pointToLayer and onEachFeature.
Upvotes: 0
Views: 1291
Reputation: 10008
Ref: 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)
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 ...)
Note that you can't draw markers with onEachFeature but you can use pointToLayer to bind popup to Markers
Upvotes: 2