Set active layer on layer control added to map

945 views
Skip to first unread message

Addam Hardy

unread,
May 30, 2014, 2:48:42 PM5/30/14
to leafl...@googlegroups.com
Hi all, 

I'm unable to find a method (or an initialization option) to select a layer as active when you add a control to a map. Everything else is working great. The control is added and then I can select a base layer to display, but I would really like to have one of them pre-selected on load. Code and screenshot below. 

Thanks for your help

Addam Hardy

      var positive = L.heatLayer(positive_points, {radius: 20});
      var negative = L.heatLayer(negative_points, {radius: 20});
      var point_group = L.layerGroup();
      var overlays = {
          "Positive": positive,
          "Negative": negative,
          "Color Points": point_group
      };
     this.control = L.control.layers(overlays, null,{collapsed: false}).addTo(mapObject);



Historical Atlas

unread,
May 30, 2014, 3:11:39 PM5/30/14
to leafl...@googlegroups.com
Hello,

You just need to add it either
a) while creating your map object with the layers option, or
b) add it after map object creation
example:
a) map = L.map(...
     layers: [positive],
...);
or
b) map.addLayer(positive)

As you might have guessed you can add as many layers as you wish...
--

---
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+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Addam Hardy

unread,
May 30, 2014, 3:15:37 PM5/30/14
to leafl...@googlegroups.com
I would have bet money that I tried that without success… but just worked straight away with your method so I guess I had not tried it. :) Thanks for your time!

You received this message because you are subscribed to a topic in the Google Groups "Leaflet" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/leaflet-js/qbComREyoHI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to leaflet-js+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages