Integrate Jawg with Mapbox Android SDK

Estimated reading time: 3 minutes

The open source Mapbox Android SDK provides a rich set of functionality for displaying and manipulating vector or raster based maps inside Android applications.

Using Mapbox Android SDK

To use Mapbox Android SDK, add the Maven Central repository and the following dependency to the dependencies section of your build.gradle:

repositories {
  mavenCentral()
}

dependencies {
  implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.2.0'
}

Initialization

Before using the SDK you must create add a MapView to your Activity and declare a Layout.

MapView

To display your map you need to add a MapView to your activity:

public class MyMapActivity extends Activity {

    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mapbox.getInstance(this,  null);
        setContentView(R.layout.activity_main);
        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull MapboxMap mapboxMap) {
                String url = "https://tile.jawg.io/jawg-streets.json?access-token=<ACCESS-TOKEN>";
                Style.Builder builder = new Style.Builder().fromUrl(url);
                mapboxMap.setStyle(builder, new Style.OnStyleLoaded() {
                    @Override
                    public void onStyleLoaded(@NonNull Style style) {

                    }
                });
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}

Note that your activity must explicitly call the corresponding lifecycle methods of the MapView.

Your map view requires a style url to render your map. You can find your style url in your Style Manager. You can choose a default Jawg style like Jawg Streets or one of your custom style. Once the style chosen, click the Use button then the Mapbox Android SDK button.

Make sure to provide a valid access token or you won’t be able to display your map.

Layout

In your map activity layout file, declare your MapView attributes:

<com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:mapbox_uiLogo="false"
        mapbox:mapbox_uiAttribution="false"/>

For the full list of MapView attributes see here.

Attribution

Your map must display the following links: Jawg and © OpenStreetMap.