Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch from Terrain layer to Satellite layer using Mapbox

I'm using Mapbox for a dynamic map on a social website.

I added a satellite toggle button but I can't find any way in the API docs on how to switch from terrain view to satellite view like Google Maps does?

Is it hidden somewhere? I know I have to subscribe and I will but I need to know I can switch from terrain to satellite in realtime without losing my markers, etc.

Let's say I have a simple map:

var initialLocation = [40.97, 64.07];
var initialZoomLevel = 2;

var map = L.mapbox.map('map_container').setView(initialLocation, initialZoomLevel);

How could I switch from terrain to satellite?

Any suggestions?

Thanks!

like image 439
Tommy B. Avatar asked Oct 16 '25 10:10

Tommy B.


2 Answers

Its pretty easy to add a satellite view - just not very well documented. A prerequisite though is you must have a paid account (UPDATE: you used to need a paid account - that no longer seems to be the case) and have setup your own map in your account over there with the satellite imagery selected.

var initialLocation = [40.97, 64.07];
var initialZoomLevel = 2;

var map = L.mapbox.map('map_container').setView(initialLocation, initialZoomLevel);
L.control.layers({  
    "Street": map.tileLayer,
    "Satellite": L.mapbox.tileLayer("my satellite imagery map id")
}, null).addTo(map);

This adds a control in the upper right (by default) that allows you to select between the default map and your satellite map.

like image 88
David K. Hess Avatar answered Oct 18 '25 23:10

David K. Hess


With MapBox there is no concept of "modes", just different maps each with their own Map ID. So create a map at http://tiles.mapbox.com/newmap based on terrain, then another based on satellite imagery. Switch between them with the second argument to L.mapbox.map (see http://www.mapbox.com/mapbox.js/api/#L.mapbox.map) using their respective IDs.

like image 43
incanus Avatar answered Oct 19 '25 00:10

incanus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!