Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I had used the following layers in leaflet,now i want to switch between the 4 layers ,please tell the code to create a layer switcher control in leaflet

// night layer
//var osmUrl = 'http://{s}{z}/{x}/{y}.png',

//golden layer
var osmUrl = 'http://{s}{z}/{x}/{y}.png'

//black layer
//var osmUrl = 'http://{s}{z}/{x}/{y}.png',

osmAttribution = 'Map data &copy; 2012 <a href="">OpenStreetMap</a> contributors',
osm = new L.TileLayer(osmUrl, {maxZoom: 18, attribution: osmAttribution});
share|improve this question
How you read the tutorial? – R.K. Aug 25 '13 at 14:14
Welcome to GIS SE! You might want's to read this Leaflet tutorial It addresses pretty much what you need. Please read the docs before asking a question next time. – R.K. Aug 25 '13 at 14:24
R.K. you're right. I just hate saying "Read the Docs" sometimes – geomajor56 Aug 25 '13 at 14:39
@geomajor56 Yeah. I hate doing it too but this one's featured prominently in the docs. I just wish the OP went through the tutorial before asking. It's rather brief. A little effort goes a long way. – R.K. Aug 25 '13 at 14:50

First, you'll need to rename your basemaps differently:

// night layer
var night = http://{s}{z}/{x}/{y}.png',

//golden layer
var golden = 'http://{s}{z}/{x}/{y}.png'

//black layer
var black = 'http://{s}{z}/{x}/{y}.png',

Then use the Layer Control option as shown in this Leaflet Example.

I haven't tried this plugin, but it's a different type of control for selecting layers.

Hope this gets you started.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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