Questions about polyline

41 views
Skip to first unread message

Christoph Boget

unread,
May 1, 2018, 10:55:44 AM5/1/18
to Leaflet

I have some questions about leaflet's polyline and they are as follows

    • Is there a way when instantiating the polyline (L.polyline) to dictate that it should not show by default on the map? I know that I can use .addTo([map]) and .remove() but is there a way to specify as part of the options?

    • What is the best practice to hide/show polylines that are used to indicate a path between markers (L.marker)? I know that I can add the markers to a layer group (L.layerGroup). I know that when creating the polyline, I can use the same lat/lng for each of the markers indicating the path. I know that I can show/hide each one of those individually (using L.Control.Layers and passing in the layer groups in as overlay maps and using that control to show/hide the markers and then, again, using the addTo() and .remove() for the polylines) but what I'm hoping to get help with is to make it show that the polylines show/hide when the layer group is visible/not visible and also show/hide them using an external control. For the former case, I've tried using feature groups (L.featureGroup) but I could not get it to work. For that latter case am I limited to .addTo() and .remove()?

thnx,
Christoph

Peter Thomson

unread,
May 1, 2018, 12:12:05 PM5/1/18
to Leaflet

Is there any particular reason why you don't want to use .addTo[map] ?
This is the most effective way of controlling what is visible and what is not at any time.
You can control the markers and polylines by using a separate layerGroup for each. Then use any JavaScript to control when they become visible. Again, is there any reason why you don't want to use .addTo(map) ?
Peter

Christoph Boget

unread,
May 1, 2018, 12:32:47 PM5/1/18
to Leaflet
Is there any particular reason why you don't want to use .addTo[map] ?

I'm refactoring some code that an old dev left us.  Previously it was using the google maps api and I'm switching it over to leaflets (I'm new to both).  Google provided a ``visible`` property to determine at instantiation if the line should be visible by default.  In the case that it should not be visible by default, I'm going to have to create a handle on each line object (and there could be a large number of them) and add them individually (or even as sub-groups using L.layerGroup) when they should be visible and remove them individually when they should not be.  So while I can use .addTo() (or even .addLayer() to a layer group), I need to keep track of them all.  Again, a lot of this is driven by the code I'm refactoring vs. what is available in the leaflet api (e.g., addTo(map))
 
This is the most effective way of controlling what is visible and what is not at any time.

Unless there are a large number of lines.
 
You can control the markers and polylines by using a separate layerGroup for each. Then use any JavaScript to control when they become visible. Again, is there any reason why you don't want to use .addTo(map) ?

See above. 

Thank you very much for your reply.  I guess I was just hoping that there was an easier way. :/

thnx,
Christoph

Peter Thomson

unread,
May 1, 2018, 1:05:59 PM5/1/18
to leafl...@googlegroups.com
I would suggest creating an array to hold each line, and an array to hold the state of each line ( and what it should be at the start). Then have a single function to either toggle the state of any line, or to make a line visible / invisible.

You can also get the browser to remember the state of each line by using localStore, so that when a user returns to the map it is redrawn in the same state as when they closed it, without needing to store anything on the server.

--

---
You received this message because you are subscribed to the Google Groups "Leaflet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-js+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

w.n...@alf4all.demon.nl

unread,
Jul 22, 2019, 3:23:12 PM7/22/19
to Leaflet
Hallo Christoph,

I got my markers with the plug-in omnivore.csv. Can you help me how to add a polyline between the markers?

Regards...

Op dinsdag 1 mei 2018 16:55:44 UTC+2 schreef Christoph Boget:

w.n...@alf4all.demon.nl

unread,
Jul 22, 2019, 3:25:20 PM7/22/19
to Leaflet
Reply all
Reply to author
Forward
0 new messages