Introduction to Mapbox Android Learning Notes

Posted by jackinva on Sun, 04 Aug 2019 09:15:31 +0200

brief introduction

The Mapbox Maps SDK for Android is an open source toolkit for displaying maps in Android applications.
Mapbox: https://docs.mapbox.com/android/maps/overview/
Reference resources: Introduction to Mapbox Android (I)

install

1. Adding dependencies
In the build.gradle file, ensure that minSdkVersion is greater than or equal to 14, add dependence:

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

Note: After adding Mapbox Maps SDK to Android, there may be a mismatch with Gradle dependencies. Don't forget that you can use the following "exclude group" to delete certain dependencies:

implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:8.2.1'){
    exclude group: 'group_name', module: 'module_name'
}

In addition, running "gradle app_module_name_here:dependencies" on the command line prints a list of dependencies." / gradlew app:dependencies "If you have a Gradle wrapper, you can work. When multiple libraries are included in a project, they help troubleshoot flexible Gradle configurations. You can see the dependencies that specific libraries bring and where conflicts may occur.

2. Get Acess Token
Get default public token at Mapbox's account interface. Add a special token in R.strings.xml to save the token:

<string name="mapbox_access_token">MAPBOX_ACCESS_TOKEN</string>

Then, to pass it to the Maps SDK, you need to place acess token in the onCreate() method of the application.

public class MyApplication extends Application {
 
@Override
public void onCreate() {
	super.onCreate();
 
	// Mapbox Access token
	Mapbox.getInstance(getApplicationContext(), pk.eyJ1IjoiMTU5OTUwODY2MTYiLCJhIjoiY2pzanBmejBiMTh4dDQ0cDY0NDk0Y3JobCJ9.sEmUKARVlNUTVbbVbrwGAw);
	}
}

If you want to switch Mapbox access tokens at runtime, the Maps SDK also provides a setToken() method. Some Mapbox APIs require special Mapbox tokens, such as Chinese map s. Setting up a new token can use multiple Mapbox tools at the same time. This approach allows you to set tokens before using a specific Mapbox tool, rather than initially, and requires the same token for all Mapbox-related requests. As follows:

Mapbox.setAccessToken(pk.eyJ1IjoiMTU5OTUwODY2MTYiLCJhIjoiY2pzanBmejBiMTh4dDQ0cDY0NDk0Y3JobCJ9.sEmUKARVlNUTVbbVbrwGAw);

3. Setting permissions
Add the appropriate permissions to the Manifest file:

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

4. Add Maps to Applications
First, the mapview component is introduced into activity_main.xml:

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

Then, the following examples show how to add maps to activity, including creating activities, setting token, importing styles, and designing map components.
MainActivity.java:

private MapView mapView;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	Mapbox.getInstance(this, pk.eyJ1IjoiMTU5OTUwODY2MTYiLCJhIjoiY2pzanBmejBiMTh4dDQ0cDY0NDk0Y3JobCJ9.sEmUKARVlNUTVbbVbrwGAw);

	setContentView(R.layout.activity_main);

	mapView = (MapView) findViewById(R.id.mapView);
	mapView.onCreate(savedInstanceState);
	mapView.getMapAsync(new OnMapReadyCallback() {
	@Override
	public void onMapReady(@NonNull MapboxMap mapboxMap) {

		mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
		@Override
		public void onStyleLoaded(@NonNull Style style) {
	  		// Map is now set up and style loaded. Data can be added here or other map adjustments can be made.
	  		
		}
	});
	}
});

5.Lifecycle method
MapView contains its own lifecycle approach for managing Android's OpenGL lifecycle, which must be invoked directly from the included activities. In order for your application to invoke the lifecycle method of MapView correctly, you must override the following lifecycle method in the activity that contains MapView and invoke the corresponding MapView method. A total of lifecycle methods under override are required, including matching MapView methods.

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

For example, the onStart() method should look like this:

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

Note that if fragment is used, then mapview.onDestroy() is called in the onDestroyView() method of fragment, rather than in the onDestroy() method. As follows:

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

Attribution Attribution

You must include Mapbox wordmark and attribute notifications on any map that uses the Mapbox Maps SDK for Android. SDK provides a property layout that contains all the necessary information, which can be customized using xml or uisetings objects. You can adjust the location of Mapbox word marks and attribute notifications, but they must remain visible on the map. You can also change the background and text color of attribute notifications to match your design aesthetics, but all information must be clear and readable. Otherwise, you can't change Mapbox word mark or text attribute notification. If you want to move or delete Mapbox wordmark, please contact our sales team to discuss the options available under our enterprise plan.

Telemetry

Mapbox Telemetry is a powerful location analysis platform included in this SDK. By default, SDK sends anonymous location and usage data to Mapbox whenever the host application requests to collect anonymous location and usage data. Mapbox Terms of Service require your application to provide users with a way to choose individually not to use Mapbox telemetry technology, which is part of the automatic attribute control provided. If the property control is hidden, an optional out option must be provided to the user.

XML attributes

ML attributes can be added to the XML MapView to further customize map behavior, such as setting the starting camera position, enabling tilt or adjusting the compass position on the screen. All MapView XML attributes begin with "mapbox_" to identify and eliminate any potential conflicts with other libraries. Because of the current implementation of Android Studio, you cannot automatically generate MapView properties by typing. Examples of some of these attributes are as follows:

<com.mapbox.mapboxsdk.maps.MapView
  mapbox:mapbox_cameraTargetLat="-36.84"
  mapbox:mapbox_cameraTargetLng="174.76"
  mapbox:mapbox_cameraZoom="10"
  mapbox:mapbox_cameraBearing="34.33"
  mapbox:mapbox_cameraTilt="50.25"
  mapbox:mapbox_cameraZoomMax="12.41"
  mapbox:mapbox_cameraZoomMin="6"
  mapbox:mapbox_uiRotateGestures="false"/>

Topics: Android SDK xml Gradle