Build native applications on Android devices

An open source toolset for building mapping applications for Android devices with great flexibility for visual styling and customizability.

Mapbox Android SDK4.1.1
Installation Supports API level 15 or higher

API overview

This guide will take you through the process of adding a map to your Android app. It assumes you have a Java IDE (like Android Studio) with the Android SDK installed, an application project open, and general familiarity with building User Interfaces in Android. A demo app is available on the Google Play Store that includes all the examples.

App permissions and services

Ensure the following core permissions are requested in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

If the app is targeting Android Marshmallow (API 23) or later it's necessary to request permissions at runtime.

While your AndroidManifest.xml file is open, you should also include the telemetry service:

<service android:name="com.mapbox.mapboxsdk.telemetry.TelemetryService"/>

MapView and MapboxMap

MapView and MapboxMap are the key components of the Mapbox Android SDK. MapView behaves like any other View and its behavior can be changed statically with an XML layout file, or programmatically during runtime. You can think of MapboxMap as the controller class for your map. It includes methods to set and move the camera position, add markers, configure user interatcions, and more.

Lifecycle methods

The MapView contains its own lifecycle methods for managing Android's OpenGL lifecycle that must be called directly from the containing Activity. In order for your app to correctly call the MapView's lifecycle methods, you must override the following lifecycle methods in the Activity that contains the MapView:

onCreate();
onResume();
onPause();
onSaveInstanceState();
onLowMemory();
onDestroy();

XML layout

To add the MapView as a layout element, add the following to your xml file:

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
mapbox:style_url="@string/style_mapbox_streets"

And then you can call it programmatically within an Activity:

findViewById(R.id.mapview);

Access tokens

An access token is necessary to use Mapbox services and APIs, such as maps, directions, and geocoding. Your access tokens can be managed in your account settings, where you can retrieve current tokens and generate new ones. You should create a new token for each of your apps, which will help you track usage and minimize disruption in the event a token needs to be revoked.

There are two places to provide an access token in your app. If you are using a custom application object, you can place it in the onCreate() or it can go in your applications launch acitvity onCreate(). In either case you'll need to call the MapboxAccountManager and pass it context and your access token:

  MapboxAccountManager.start(context, <your access token here>);
  

Telemetry opt out

Mapbox Telemetry is a powerful location analytics platform included in this SDK. By default, anonymized location and usage data is sent to Mapbox whenever the host app causes it to be gathered. The Mapbox Terms of Service require your app to provide users with a way to individually opt out of Mapbox Telemetry, which is provided automatically as part of the attribution control. If you hide the attribution control, you must provide an alternative opt out for your users to use.

Map styles

The Mapbox Android SDK is designed to work with the custom styles you create in Mapbox Studio, but also comes bundled with five professional styles that will look great in your app:

  • Mapbox Streets: Mapbox Streets is a comprehensive, general-purpose map that emphasizes accurate, legible styling of road and transit networks.
  • Outdoor: Mapbox Outdoors is a general-purpose map with curated datasets and specialized styling tailored to hiking, biking, and the most adventurous use cases..
  • Light and Dark: Mapbox Light and Mapbox Dark are subtle, full-featured maps designed to provide geographic context while highlighting the data on your analytics dashboard, data visualization, or data overlay.
  • Satellite: is our full global base map that is perfect as a blank canvas or an overlay for your own data.
  • Satellite Streets: combines our Mapbox Satellite with vector data from Mapbox Streets. The comprehensive set of road, label, and POI information brings clarity and context to the crisp detail in our high-resolution satellite imagery.

To use a custom style, paste your style URL into your MapView's mapbox:style_url attribute in your activity's layout file. If you would like to change the map style later on, call MapboxMap.setStyleUrl() with the new style and your map will update.

Attribution

You must comply with the licensing terms of any map data in your application, including Mapbox Streets or other Mapbox maps if used. A small attribution control will be displayed on the map view automatically. The attribution control may be moved or removed as necessary, so long as the required attribution is reasonably provided in your app.

For more information on Mapbox’s attribution requirements, click here.

Supported Android versions

The Mapbox Android SDK is supported for apps running Android API 15 (Ice Cream Sandwich MR1) and above. To ensure that it works in your app, please set minSdkVersion in the App Manifest to no lower than 15.

<uses-sdk android:minSdkVersion="15"
          android:targetSdkVersion="integer"
          android:maxSdkVersion="integer" />

Include Mapbox Android Services in your application

An open source toolset to consume Mapbox APIs in your Java and Android applications. Some of the APIs include directions, geocoding, map matching, and static map imagery. Two libraries are at your disposal: a Java library compatible with any Java application and an Android library intended to be used specifically for Android applications.

Mapbox Service SDK1.2.1
Installation Libraries

Mapbox Android Services (MAS)

An overview of all the APIs this SDK exposes is listed below along with information on getting started. To see these APIs in action, download the Mapbox Demo App today from the Google Play Store. If you are using this library in an Android application, you will need to include one core permission in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />

Geocoder

Using Mapbox Android Services, you are only a few steps away from adding geocoding functionality to your application. This includes both forward geocoding (providing an address and receiving coordinates) and reverse geocoding (providing coordinates and recieving an address). This SDK also includes an autocomplete widget for use in Android applications (check out this example).

Directions

MAS can also request driving, cycling, or walking directions from the Mapbox Directions API. Include an origin, destination, and any additional stops along the way and the API will find the most efficent route. You can also specify additional options along with your directions request. For example, request alternative routes with .setAlternatives(), ensure the route progresses straight with .setContinueStraight(), and much more!

Check out the Mapbox Directions API documentation and this directions example to learn more.

Static maps

The Static API returns standalone map images that can be displayed without the aid of the Mapbox Android SDK. They look like an embedded map without interactivity or controls. To display an interactive map in an Android application, use a third-party library such as Picasso or Volley to handle the image request.

Check out the Mapbox Static API and this static map example to learn more.

Map matching

Many GPS traces, whether from a smartphone or standalone GPS device, are imprecise and won't perfectly match the the OpenStreetMap road or path network in Mapbox maps. With the Mapbox Map Matching API, you can snap a GPS route to Mapbox's road and path network, resulting in a clean route.

Check out the Map Matching API documentation and this map matching example to learn more.

Turf and other utilities

Mapbox Android Services includes a few powerful methods to analyze and transform your map data. You can use Turf to calculate bearing, distance, and even a destination. If you need to extract a smaller section of a polyline or linestring, use the Turf line slice method now found in the SDK.

Another utility, polyline simplification, can be used to reduce the number of points in a polyline while retaining its shape. This can improve performance and reduce in visual noise.

Need help?

Our Help page is filled with how-to references and guides so you can master our tools.