23

I have a back to home function

function panToHome(){
    latLng = [current.lat, current.lng];
    map.setView(latLng, 8);
}

I want to save the current view as history, so user can switch back as they might click mistakely. So the question is how can I know the current latlng on Mapbox?!

Weijing Jay Lin
  • 2,991
  • 6
  • 33
  • 53

3 Answers3

29

Use the getZoom method on your L.mapbox.map instance:

Returns the current zoom of the map view.

http://leafletjs.com/reference.html#map-getzoom

var mapbox = new L.mapbox.map('mapbox', 'mapbox.streets', {
    'center': [0, 0],
    'zoom': 9
});

var zoom = mapbox.getZoom();

Here zoom holds 9

iH8
  • 27,722
  • 4
  • 67
  • 76
  • 1
    why this reference leafletjs if the question is about mapbox? – shinzou Oct 05 '20 at 14:20
  • 1
    Maybe because the question is about mapboxjs which is based on Leaflet? Even mapboxjs own documentation refers to the leaflet reference. https://docs.mapbox.com/mapbox.js/api/v3.3.1/ – iH8 Oct 08 '20 at 10:42
20

In Android you can use

int currentZoomLevel = mapboxMap.getCameraPosition().zoom;
Linh
  • 57,942
  • 23
  • 262
  • 279
  • 1
    @Moh_beh can you explain what error you get. many people use it and it work. maybe new mapbox version change something. also 1 comment is enough :D – Linh Apr 30 '19 at 23:14
  • yes in new version it dosnt work but you can use this ***String me = map.getCameraPosition().toString();*** it return a lot of things include zoom level. – Moh_beh May 03 '19 at 06:17
1

On iOS with Mapbox SDK 4.0, there's a .zoomLevel property on the MGLMapView object.

bmt22033
  • 6,880
  • 14
  • 69
  • 98