Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapbox Android SDK 4.0.0+ - how to set zoom & center map on given location?

Tags:

android

mapbox

I'm using Mapbox Android SDK v4.0.0 in an Android app that at some point needs to "freeze" a MapView at a given position and a given zoom level.

It seems to me that there are no methods in the API to:

  1. Set a specific zoom level for a map,
  2. Center a map on a given location.

How to get this done?

like image 335
pstobiecki Avatar asked Apr 16 '16 08:04

pstobiecki


People also ask

What are the Mapbox SDKs for iOS and Android?

The Mapbox Maps SDKs for iOS and Android allow you to add interactive maps to your mobile applications, define custom and dynamic style rules, manipulate custom data, and interface with Mapbox web APIs.

What is an Mapbox map?

Mapbox maps do away with the basemap-overlay strategy and treat everything on the map as a layer that you can style independently and move up and down the rendering stack.

What's new in Android SDK 4?

4 major features for the new Android SDK. Our new Android SDK launches today, baking in support for offline maps, intelligent telemetry, better map interactions, and improved compatibility with the Google Maps Android API. Offline maps. Your app can now download arbitrary regions of the globe for offline use.

What is the Mapbox styles API?

The Mapbox Styles API lets you read and change map styles, fonts, and images. This API is the basis for Mapbox Studio. If you use Studio, Mapbox GL JS, or the Mapbox Mobile SDKs, you are already using the Styles API. This documentation is useful for software developers who want to programmatically read and write these resources.


1 Answers

It seems the API has changed between versions 3.2.0 and 4.0.0. Currently, to achieve the above goal, you need to use a CameraPosition and inject it into your mapboxMap:

mapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(MapboxMap mapboxMap) {
        mapboxMap.setCameraPosition(new CameraPosition.Builder()
                .target(yourLatLng)
                .zoom(yourZoom)
                .build());
    }
});

Works like a charm.

like image 64
pstobiecki Avatar answered Nov 14 '22 23:11

pstobiecki