1

How do I add multiple layers to the leaflet map. I have added the overlay_tile to the tilelayer but nothing happens. tilelayer = L.tileLayer(url_tile,overlay_tile, {noWrap.... My example can only have one tile at a time. When I add multiple tiles to the tileLayer nothing happens.

What I have Leaflet Example What I am trying to achieve. NASA Example

code

 var url_tile='https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png';
 url_tile='//gibs-{s}.earthdata.nasa.gov/wmts/epsg4326/best/MODIS_Terra_CorrectedReflectance_TrueColor/default/2020-05-05/EPSG4326_250m/{z}/{y}/{x}.jpg'

 var overlay_tile='//gibs.earthdata.nasa.gov/wmts/epsg3857/best/MODIS_Terra_Aerosol/default/2021-06-05/GoogleMapsCompatible_Level6/{z}/{y}/{x}.png';
 var map = L.map('map', {editable: true}).setView(startPoint, 2),
 tilelayer = L.tileLayer(url_tile,
 { noWrap: true,
   maxZoom: 20, attribution: 'Data \u00a9 <a href="https://www.openstreetmap.org/copyright"> OpenStreetMap Contributors  </a> Tiles \u00a9 HOT'
 }).addTo(map);
2
  • 1
    Coding question on GIS SE site should include relevant existing code, otherwise it's most likely to be closed as not compliant with the site policy. Q/A on GIS SE site are not intended only to solve problem of particular user, but be helpful resource for anybody with similar questions/problems. That's the reason question has to be complete in itself and include all relevant code, since outside links tend to get lost in time and also that there is no need to visit other sites to get complete picture of what the problem/question is. Please edit your question and add relevant existing code.
    – TomazicM
    Commented Jun 24, 2021 at 15:45
  • @TomazicM added relevant code. I am new to gis so type of work so i am a little lost. Commented Jun 24, 2021 at 16:11

1 Answer 1

1

There are few things you have to correct in your code to make it work as desired.

First there is definition of tile layer url url_tile. You first define it as OpenStreet tile layer source, and right after that assign to it some other layer source, which of course overrides the first one. Besides that that second source is based upon coordinate system EPSG:4326, which is different from your desired overlay source, which is based upon standard coordinate system EPSG:3857 used in web maps. In Leaflet you cannot mix layers with different coordinate systems, all layeers must use the same one (see map option crs in https://leafletjs.com/reference-1.7.1.html#map-factory). So second source wouldn't work correctly anyway.

Then, to put you overlay on the map, you just have to define layer for it the same way as the base layer. Since it is raster layer and it overlays the base map, it's usual practice to make it a bit transparent with the opacity option.

So your code could then look something like this:

var url_tile = 'https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png';
var overlay_tile = 'https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/MODIS_Terra_Aerosol/default/2021-06-05/GoogleMapsCompatible_Level6/{z}/{y}/{x}.png';

var map = L.map('map', {editable: true}).setView(startPoint, 2);

var tilelayer = L.tileLayer(url_tile, {
  noWrap: true,
  maxZoom: 20,
  attribution: 'Data \u00a9 <a href="https://www.openstreetmap.org/copyright"> OpenStreetMap Contributors </a> Tiles \u00a9 HOT'
}).addTo(map);

var overlaylayer = L.tileLayer(overlay_tile, {
  noWrap: true,
  opacity: 0.6
}).addTo(map);
3
  • Thanks i just realized the coordinated systems where incompatible. Is it standard practice have var overlaylayer=L. ? Or just add L.tileLayer( Commented Jun 24, 2021 at 19:49
  • If you don't need layer object later, usually the second possibility is used.
    – TomazicM
    Commented Jun 25, 2021 at 6:37
  • Thanks for you help and advice. Commented Jun 25, 2021 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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