Add layers using Leaflet

If you want to add a layer to a map created with Leaflet, for example, if you want to add a satellite-view layer (ortophoto):

javascript
// Defining API link to get the tiles
var mapboxURL = 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=${access_token}';

// Defining both layers and defining tiles
var streets   = L.tileLayer(mapboxURL, {id: 'mapbox/streets-v11', tileSize: 512, zoomOffset: -1, attribution: 'Mapbox'});
var ortophoto = L.tileLayer(mapboxURL, {id: 'mapbox/satellite-streets-v11', tileSize: 512, zoomOffset: -1, attribution: 'Mapbox'});

// Adding tiles to the map
var map = L.map(element, {
    zoomControl: false,
    layers: [streets, ortophoto]
});

// Defining the layers as base maps
var baseMaps = {
    "Ortophoto": ortophoto,
    "Streets": streets
}

// Adding layers to the map
L.control.layers(baseMaps).addTo(map);

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.