I have a leaflet map of the US and I would like to toggle between zip codes and county shapes. There seems to be no tutorials on how to achieve this in the documentation or an other forums I have looked at.


If I understand what you are asking, you have a basemap of the US, and geojson layers for zip codes and county shapes, and you want to toggle (turn on or off) the geojson layers?

Leaflet layer control is designed for this



Leaflet L.geoJson() returns a layer which you can use in the layers control:

// parse the geojson (very simplified example here)
var zip = L.geoJson(zipjson, {});
var counties = L.geoJson(countiesjson, {});

// from there, similar to the example: 
var usmap = L.tileLayer(url, {});

// initialize the map
var map = L.map('map', {
    center: [39.73, -104.99],
    zoom: 10,
    layers: [usmap]

// specify the basemap and overlays to put in the layers control
var baseMaps = {
    "US": usmap,

var overlayMaps = {
    "Counties": counties,
    "Zips": zip

// initialize up the L.control.layers
L.control.layers(baseMaps, overlayMaps).addTo(map);
  • I saw this and I assumed this only toggles between different maps rather than json files with polygon boundaries. As you know, the boundaries for zip code and counties are different so I'm trying to figure out a way to switch between them without having to make a separate map. Am I wrong in assuming this? If so, I can take a further look. Thanks! – Minh Mai May 28 '15 at 18:21
  • The control toggles between layers. L.geoJson returns a layer you can use in the layer control. I've updated the answer – toms May 28 '15 at 18:35
  • Just read your comment again."boundaries for zip code and counties are different". Are you saying the zip code and counties data have a different projection, or do they have a different extent? There are many sources for zip codes and counties that cover the entire US... – toms May 28 '15 at 18:36
  • Forgive me, I am unaware of projections vs extent. The overall shape of the zip and counties are the same, it's just that there are different breakdown within it. I hope that's not too confusing. – Minh Mai May 28 '15 at 18:54
  • that makes sense; should work, as shown above. – toms May 28 '15 at 19:30

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.