This question arises because on my project I will have a backend that consists of gpx tracks. Then, my frontend queries it (based on time, location filters, etc.) and I will either get the corresponding tracks and show them on a leaflet map or some specific points to also show on that map.

Then, I need to be able to style those tracks and be able to interact with them. Is it easier to achieve this through PolyLines or GeoJSON layers?


The L.geoJson() constructor is actually just a convenient tool to convert GeoJSON data into Leaflet vector / path object(s) (like L.polyline, L.polygon, etc.).

It also attaches the extra information that may be contained in the GeoJSON data into the created Leaflet objects (in layer.feature.properties for example).

Therefore, if the GPS track that you retrieve from your server is not already formatted in GeoJSON, then there is little value in converting it first into GeoJSON and then using L.geoJson, compared to building directly the corresponding L.polyline.

But it also depends on your overall application, if you already have other utilities that use GeoJSON data, etc.

  • I intend to format the data to GeoJSON on my backend and then send it, so I will retrieve the information in GeoJSON for the tracks/points and plain JSON for other information. I still have to give a proper look on my backend (will do it in the next weeks) but even for individual points I will code them into GeoJSON format. With this being said, and using your answer, L.geoJson is more useful. Also, is L.geoJson good enough to allow interactivity with the tracks? (select, highlight, etc.). Thanks. – Shoplifter20 May 6 '16 at 17:34
  • As said in the answer, the L.geoJson() will build L.polyline for you, on which you can use setStyle() on "mouseover", "mouseout" events to create highlighting, and similarly for selecting. With the L.geoJson() constructor, you have options (like onEachFeature) that facilitate binding such events on the built path objects at once. See the Leaflet GeoJSON tutorial. – ghybs May 7 '16 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.