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:5.3.1'
}

You should ensure your minSdkVersion is >= API 14.

Initialization

Before using the SDK you must initialize the Mapbox instance in your Application class.

But there is a catch: The class method Mapbox#getInstance requires an access token that starts with “pk.” or “sk.” (see this).

Since the access token will be provided in the style url (see below) we can simply provide a dummy access token that starts with “pk.”:

public class MyApplication extends Application {
 
  @Override
  public void onCreate() {
    super.onCreate();
    Mapbox.getInstance(getApplicationContext(), "pk.dummy");
  }
}

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);
        
        setContentView(R.layout.my_map_activity);

        mapView = findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
    }
    
    @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.

In your map activity layout file set the mapbox_styleUrl attribute:

<com.mapbox.mapboxsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        mapbox:mapbox_styleUrl="https://tile.jawg.io/jawg-streets.json?access-token=HN9vnGVNz3tmIU0DYyJBeGVBXZpzv3fGn0u4tmTh7DyLyDBhbTtLJHYNDS2bLC2h"
        mapbox:mapbox_uiLogo="false"
        mapbox:mapbox_uiAttribution="false"/>

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

For the full list of MapView attributes see here.

Attribution

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