Simple MapView

This example shows how to display a simple map view in activity, this map view does not contain complicated functions, only basic UI.

Simple MapView

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

activity_simple_map_view.xml view source

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>

SimpleMapViewActivity view source

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
}
24
25
@Override
26
public void onMapReady(@NonNull NextbillionMap nextbillionMap) {
27
mMap = nextbillionMap;
28
mMap.getStyle(new Style.OnStyleLoaded() {
29
@Override
30
public void onStyleLoaded(@NonNull Style style) {
31
}
32
});
33
}
34
35
///////////////////////////////////////////////////////////////////////////
36
// Lifecycle
37
///////////////////////////////////////////////////////////////////////////
38
39
@Override
40
protected void onStart() {
41
super.onStart();
42
mapView.onStart();
43
}
44
45
@Override
46
protected void onResume() {
47
super.onResume();
48
mapView.onResume();
49
}
50
51
@Override
52
protected void onPause() {
53
super.onPause();
54
mapView.onPause();
55
}
56
57
@Override
58
protected void onStop() {
59
super.onStop();
60
mapView.onStop();
61
}
62
63
@Override
64
protected void onSaveInstanceState(@NonNull Bundle outState) {
65
super.onSaveInstanceState(outState);
66
mapView.onSaveInstanceState(outState);
67
}
68
69
@Override
70
protected void onDestroy() {
71
super.onDestroy();
72
mapView.onDestroy();
73
}
74
75
@Override
76
public void onLowMemory() {
77
super.onLowMemory();
78
mapView.onLowMemory();
79
}
80
}

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 map view and sets the map's style. The map view is initialized by calling the setContentView() method and passing it to the R.layout.activity_simple_map_view layout resource. The map's style is set by calling the mMap.getStyle() method and pass it to a listener that will be called when the style is loaded.

  • The onMapReady() method is called when the map is ready to be used. It sets the map's center and zoom level. The map's center is set by calling the mMap.setCenter() method and passing it the coordinates of the center point. The map's zoom level is set by calling the mMap.setZoom() method and pass it to the zoom level.

  • 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.

© 2024 NextBillion.ai all rights reserved.