最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - combining overlays in mapboxleaflet layers control - Stack Overflow

matteradmin6PV0评论

I'm trying to bine 2 or more overlays into one overlay checkbox. I'm using leaflet layers control with mapbox.js 1.6 to toggle my overlays. It doesn't matter to me if I bine them on mapbox into one data layer, or if I bine separate data layers in my JS code into one overlay checkbox, but I can't seem to do either. I'm exporting MBTiles from Tilemill to my Mapbox account.

Note that it's not an option to:

  • bine them in Tilemill (the single zoom level and square bounding box won't work for me across all layers)
  • add the various data layers to a single map project on Mapbox (I'd like it to be toggleabe by the user)

I'm trying to bine 2 or more overlays into one overlay checkbox. I'm using leaflet layers control with mapbox.js 1.6 to toggle my overlays. It doesn't matter to me if I bine them on mapbox. into one data layer, or if I bine separate data layers in my JS code into one overlay checkbox, but I can't seem to do either. I'm exporting MBTiles from Tilemill to my Mapbox account.

Note that it's not an option to:

  • bine them in Tilemill (the single zoom level and square bounding box won't work for me across all layers)
  • add the various data layers to a single map project on Mapbox. (I'd like it to be toggleabe by the user)
Share Improve this question edited Jul 25, 2014 at 9:04 sehop asked Jul 25, 2014 at 8:48 sehopsehop 951 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can use L.layerGroup to bine layers

var group = L.LayerGroup([layer1, layer2];

// add default layers to map
map.addLayer(layer1);

// switcher
var baseLayers = {
    "My Group": group,
    // more layers
};

// add layer groups to layer switcher control
var controlLayers = L.control.layers(baseLayers).addTo(map);

You may be interested in this thread Leaflet layer control for basemap group layers

You can use the L.control.layers with the L.layerGroup. Here is the JSFiddle I wrote for this. You can add as many base or overlay layers you want.

Once you create the L.layerGroups, define the base and the overlay layers and add them to the control like this:

var controlLayers = L.control.layers(baseLayers, overlayMaps).addTo(map);
Post a comment

comment list (0)

  1. No comments so far