Getting Started

The Getting Started section of the Android Maps SDK is specifically crafted to facilitate a seamless and efficient onboarding process for developers. It provides a comprehensive guide that outlines the prerequisites, requirements, and necessary steps to start utilizing the SDK effectively. From installation instructions to information on Maven dependencies, this section offers detailed guidance to ensure a smooth integration of the Android Maps SDK into developers' projects.

By following the step-by-step instructions and recommendations provided in this section, developers can swiftly set up the Android Maps SDK and begin constructing captivating maps for their users. Additionally, the section includes a Quickstart guide that offers a concise and practical demonstration of how to create a basic map using the SDK.

Prerequisites

  • Access Key: if you don’t have your Access Key yet, don’t hesitate to contact us
  • IDE (Android Studio): to build and develop the Android application
  • There are no restrictions on IDE version, but we recommend developers to use Arctic Fox | 2020.3.1 and above.

Installation

The guide explains how to install the SDK by adding the necessary dependencies to your project. It provides instructions on adding the maven dependencies of SDK to your app's build.gradle file.

Maven dependencies

In your app-level build.gradle file, add the Nextbillion.ai dependency for the Maps SDK for Android.

1
dependencies {
2
def version = "3.1.1"
3
implementation 'ai.nextbillion:nb-maps-android:$version'
4
}

This line tells Gradle to download Nextbillion Android Maps SDK version 3.1.1 (or the version defined in the version variable) from Maven Central and include it in your app build.

Initialization

Before we start using any functionalities from both SDK, we need to initialize the Maps SDK first, by initializing it means to pass your Access Key to the singleton method of the class Nextbillion.

A common way to initialize the SDK is to put the following code in an Application’s onCreate() callback, otherwise, please make sure you have called the code below before you start using any functionalities of the SDK.

1
Nextbillion.getInstance(getApplicationContext(), "your access key");

Access Key

The maps SDK doesn’t enforce users to configure the access key in any strict manner, users can maintain and store their key in the below ways:

  1. Local file(xml, Sharedpreference, or file)
  2. Database
  3. Cloud/Backend server
  4. More

For instance, if you prefer to configure the access key in an XML file, you can do so like this:

1
<string name="nbmap_access_key">your-access-key</string>

Once you have your access key configured, you can pass the value to the SDK as follows:

1
Nextbillion.getInstance(getApplicationContext(), "your access key");

This flexibility allows you to adapt the access key configuration to your specific use case and security requirements.

Base URI

By default, the Maps SDK reads the base URI from nbmap_apiBaseUri, and all MapView instances use this value to request styles and tiles.

1
<string name="nbmap_base_uri">http://api.nextbillion.io</string>

If we want to override the base Uri for a particular MapView, there are two ways:

  • Set app:nbmap_apiBaseUri in the layout XML file.

    1
    <ai.nextbillion.maps.core.MapView
    2
    android:id="@+id/mapView"
    3
    android:layout_width="match_parent"
    4
    android:layout_height="match_parent"
    5
    app:nbmap_basrUri="https://api.nextbillion.io" />
  • Set apiBaseUri in NextbillionMapOptions

    1
    NextbillionMapOptions options = NextbillionMapOptions.createFromAttributes(this, null)
    2
    .apiBaseUri("https://api.nextbillion.io");
    3
    mapView = new MapView(this, options);

Quickstart Guide

In this guide, we will walk you through the process of displaying a simple map view in an activity. The focus of this example is to provide a basic UI without complicated functions. By following this guide, you will be able to quickly set up and showcase a clean and straightforward map view in your Android application. Let's get started!

For all code examples, refer to Android Maps SDK Code Examples

1
<?xml version="1.0" encoding="utf-8"?>
2
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
xmlns:app="http://schemas.android.com/apk/res-auto"
4
xmlns:tools="http://schemas.android.com/tools"
5
android:layout_width="match_parent"
6
android:layout_height="match_parent"
7
tools:context=".MainActivity">
8
9
<ai.nextbillion.maps.core.MapView
10
android:id="@+id/simple_map_view"
11
android:layout_width="match_parent"
12
android:layout_height="match_parent"
13
app:nbmap_uiAttribution="false"
14
app:nbmap_cameraTargetLat="53.550813508267716"
15
app:nbmap_cameraTargetLng="9.992248999933745"
16
app:nbmap_cameraZoom="12" />
17
18
</androidx.constraintlayout.widget.ConstraintLayout>
1
package ai.nextbillion;
2
3
import android.os.Bundle;
4
5
import androidx.annotation.NonNull;
6
import androidx.appcompat.app.AppCompatActivity;
7
import ai.nextbillion.maps.core.MapView;
8
import ai.nextbillion.maps.core.NextbillionMap;
9
import ai.nextbillion.maps.core.OnMapReadyCallback;
10
import ai.nextbillion.maps.core.Style;
11
12
public class SimpleMapViewActivity extends AppCompatActivity implements OnMapReadyCallback {
13
14
private MapView mapView;
15
private NextbillionMap mMap;
16
17
@Override
18
protected void onCreate(Bundle savedInstanceState) {
19
super.onCreate(savedInstanceState);
20
setContentView(R.layout.activity_simple_map_view);
21
mapView = findViewById(R.id.simple_map_view);
22
mapView.onCreate(savedInstanceState);
23
mapView.getMapAsync(this)
24
}
25
26
@Override
27
public void onMapReady(@NonNull NextbillionMap nextbillionMap) {
28
mMap = nextbillionMap;
29
mMap.getStyle(new Style.OnStyleLoaded() {
30
@Override
31
public void onStyleLoaded(@NonNull Style style) {
32
}
33
});
34
}
35
36
///////////////////////////////////////////////////////////////////////////
37
// Lifecycle
38
///////////////////////////////////////////////////////////////////////////
39
40
@Override
41
protected void onStart() {
42
super.onStart();
43
mapView.onStart();
44
}
45
46
@Override
47
protected void onResume() {
48
super.onResume();
49
mapView.onResume();
50
}
51
52
@Override
53
protected void onPause() {
54
super.onPause();
55
mapView.onPause();
56
}
57
58
@Override
59
protected void onStop() {
60
super.onStop();
61
mapView.onStop();
62
}
63
64
@Override
65
protected void onSaveInstanceState(@NonNull Bundle outState) {
66
super.onSaveInstanceState(outState);
67
mapView.onSaveInstanceState(outState);
68
}
69
70
@Override
71
protected void onDestroy() {
72
super.onDestroy();
73
mapView.onDestroy();
74
}
75
76
@Override
77
public void onLowMemory() {
78
super.onLowMemory();
79
mapView.onLowMemory();
80
}
81
}

Code summary

The SimpleMapViewActivity class extends the AppCompatActivity class and implements the OnMapReadyCallback interface. It can be used to display a map view in an Android app.

The class has the following members:

  • mapView: A MapView object that is used to display the map.
  • mMap: A NextbillionMap object that represents the map data.

The class overrides the following methods:

  • onCreate(): This method is called when the activity is first created. It initializes the map view and sets the map's style.
  • onMapReady(): This method is called when the map is ready to be used. It sets the map's center and zoom level.
  • onStart(): This method is called when the activity starts. It starts the map view.
  • onResume(): This method is called when the activity resumes. It resumes the map view.
  • onPause(): This method is called when the activity pauses. It pauses the map view.
  • onStop(): This method is called when the activity stops. It stops the map view.
  • onSaveInstanceState(): This method is called when the activity is saved to the state. It saves the map view's state.
  • onDestroy(): This method is called when the activity is destroyed. It destroys the map view.
  • onLowMemory(): This method is called when the device is low on memory. It reduces the map view's memory usage.

Here are some additional details about the code:

  • The onCreate() method initializes the MapView by inflating the layout and calling mapView.onCreate(...). The style should be set in onMapReady() using setStyle(...). Note that mMap.getStyle(...) reads the current style instead of setting one.
  • The onMapReady() method is called when the map is ready to be used. In this method, set style and optionally configure camera center/zoom via CameraUpdateFactory + moveCamera/easeCamera/animateCamera.
  • The other methods in the class implement the lifecycle methods for an activity. These methods are called when the activity starts, pauses, stops, resumes, is destroyed or is low on memory.