Basic Location Tracking
This example shows how to Track the Current Location Manually
-
Location Permissions Handling
-
Tracking current location automatically when Map ready
-
Add CameraTracking Change Listener to update tracking status
-
Click Location Tracking Button to track the current location

For all code examples, refer to Android Maps SDK Code Examples
activity_basic_location_tracking.xml view source
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 android:id="@+id/coordinator_layout"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="vertical">
8
9 <ai.nextbillion.maps.core.MapView
10 android:id="@+id/mapView"
11 android:layout_width="match_parent"
12 android:layout_height="match_parent"
13 android:layout_marginBottom="0dp"
14 app:nbmap_uiAttribution="false" />
15
16 <com.google.android.material.floatingactionbutton.FloatingActionButton
17 android:id="@+id/trackLocation"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"
20 android:layout_gravity="end|bottom"
21 android:layout_marginBottom="16dp"
22 android:layout_marginEnd="16dp"
23 android:layout_marginRight="16dp"
24 android:background="@drawable/circle_white_bg"
25 android:src="@drawable/icon_location_searching"
26 android:tint="@android:color/white"
27 app:backgroundTint="@color/palette_mint_100"
28 app:layout_anchorGravity="top" />
29
30</androidx.coordinatorlayout.widget.CoordinatorLayout>
BasicLocationTrackingActivity view source
1package ai.nextbillion;
2
3import android.annotation.SuppressLint;
4import android.os.Bundle;
5import android.widget.Toast;
6
7import com.google.android.material.floatingactionbutton.FloatingActionButton;
8
9import java.util.List;
10
11import ai.nextbillion.maps.camera.CameraPosition;
12import ai.nextbillion.maps.camera.CameraUpdateFactory;
13import ai.nextbillion.maps.core.MapView;
14import ai.nextbillion.maps.core.NextbillionMap;
15import ai.nextbillion.maps.core.OnMapReadyCallback;
16import ai.nextbillion.maps.core.Style;
17import ai.nextbillion.maps.geometry.LatLng;
18import ai.nextbillion.maps.location.LocationComponent;
19import ai.nextbillion.maps.location.LocationComponentActivationOptions;
20import ai.nextbillion.maps.location.LocationComponentOptions;
21import ai.nextbillion.maps.location.OnCameraTrackingChangedListener;
22import ai.nextbillion.maps.location.modes.CameraMode;
23import ai.nextbillion.maps.location.modes.RenderMode;
24import ai.nextbillion.maps.location.permissions.PermissionsListener;
25import ai.nextbillion.maps.location.permissions.PermissionsManager;
26import androidx.annotation.NonNull;
27import androidx.appcompat.app.AppCompatActivity;
28
29public class BasicLocationTrackingActivity extends AppCompatActivity implements OnMapReadyCallback {
30
31 private MapView mapView;
32 private NextbillionMap nextbillionMap;
33 private PermissionsManager permissionsManager;
34 FloatingActionButton trackLocation;
35
36 @Override
37 protected void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setContentView(R.layout.activity_basic_location_tracking);
40
41 mapView = findViewById(R.id.mapView);
42 trackLocation = findViewById(R.id.trackLocation);
43
44 trackLocation.setOnClickListener(v -> {
45 if (nextbillionMap != null) {
46 trackLocation.setImageResource(R.drawable.ic_my_location);
47 trackLocation();
48 }
49 });
50
51 mapView.onCreate(savedInstanceState);
52
53 if (PermissionsManager.areLocationPermissionsGranted(this)) {
54 mapView.getMapAsync(this);
55 } else {
56 permissionsManager = new PermissionsManager(new PermissionsListener() {
57 @Override
58 public void onExplanationNeeded(List<String> permissionsToExplain) {
59 Toast.makeText(BasicLocationTrackingActivity.this, "You need to accept location permissions.",
60 Toast.LENGTH_SHORT).show();
61 }
62
63 @Override
64 public void onPermissionResult(boolean granted) {
65 if (granted) {
66 mapView.getMapAsync(BasicLocationTrackingActivity.this);
67 } else {
68 finish();
69 }
70 }
71 });
72 permissionsManager.requestLocationPermissions(this);
73 }
74 }
75
76 @SuppressLint("MissingPermission")
77 private void trackLocation() {
78 if (nextbillionMap == null) {
79 return;
80 }
81 LocationComponent locationComponent = nextbillionMap.getLocationComponent();
82 LocationComponentOptions locationComponentOptions = locationComponent.getLocationComponentOptions();
83 locationComponent.applyStyle(locationComponentOptions.toBuilder()
84 .pulseEnabled(true)
85 .build());
86 locationComponent.setLocationComponentEnabled(true);
87 locationComponent.setRenderMode(RenderMode.GPS);
88 locationComponent.setCameraMode(CameraMode.TRACKING);
89 }
90
91 @Override
92 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
93 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
94 permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
95 }
96
97 @Override
98 public void onMapReady(@NonNull NextbillionMap nextbillionMap) {
99 this.nextbillionMap = nextbillionMap;
100 CameraPosition cameraPosition = new CameraPosition.Builder()
101 .target(new LatLng(22.70418008712976, 78.66264025041812))
102 .zoom(14)
103 .tilt(30)
104 .tilt(0)
105 .build();
106 nextbillionMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
107 nextbillionMap.setStyle(new Style.Builder().fromUri(StyleConstants.LIGHT), this::activateLocationComponent);
108 trackLocation.setImageResource(R.drawable.ic_my_location);
109 }
110
111 @SuppressLint("MissingPermission")
112 private void activateLocationComponent(@NonNull Style style) {
113 LocationComponent locationComponent = nextbillionMap.getLocationComponent();
114
115 locationComponent.activateLocationComponent(
116 LocationComponentActivationOptions
117 .builder(this, style)
118 .useDefaultLocationEngine(true)
119 .locationComponentOptions(LocationComponentOptions.builder(this)
120 .pulseEnabled(true)
121 .build())
122 .build());
123
124 locationComponent.setLocationComponentEnabled(true);
125 locationComponent.setRenderMode(RenderMode.GPS);
126 locationComponent.setCameraMode(CameraMode.TRACKING);
127 locationComponent.addOnCameraTrackingChangedListener(new OnCameraTrackingChangedListener() {
128 @Override
129 public void onCameraTrackingDismissed() {
130 trackLocation.setImageResource(R.drawable.icon_location_searching);
131 }
132
133 @Override
134 public void onCameraTrackingChanged(int i) {
135
136 }
137 });
138
139 locationComponent.addOnLocationClickListener(
140 () -> Toast.makeText(this, "Location clicked", Toast.LENGTH_SHORT).show());
141
142 locationComponent.addOnLocationLongClickListener(
143 () -> Toast.makeText(this, "Location long clicked", Toast.LENGTH_SHORT).show());
144 }
145
146 @Override
147 protected void onStart() {
148 super.onStart();
149 mapView.onStart();
150 }
151
152 @Override
153 protected void onResume() {
154 super.onResume();
155 mapView.onResume();
156 }
157
158 @Override
159 protected void onPause() {
160 super.onPause();
161 mapView.onPause();
162 }
163
164 @Override
165 protected void onStop() {
166 super.onStop();
167 mapView.onStop();
168 }
169
170 @Override
171 protected void onSaveInstanceState(Bundle outState) {
172 super.onSaveInstanceState(outState);
173 mapView.onSaveInstanceState(outState);
174 }
175
176 @Override
177 protected void onDestroy() {
178 super.onDestroy();
179 mapView.onDestroy();
180 }
181
182 @Override
183 public void onLowMemory() {
184 super.onLowMemory();
185 mapView.onLowMemory();
186 }
187}
This code example demonstrates basic location-tracking functionality using the Nextbillion map library. It handles location permissions, tracks the current location automatically when the map is ready, adds a camera tracking change listener to update the tracking status, and allows the user to click a button to track their current location.
Location Permissions Handling:
-
The code checks if location permissions are granted using PermissionsManager.areLocationPermissionsGranted(this).
-
If permissions are granted, the map is asynchronously loaded using mapView.getMapAsync(this).
-
If permissions are not granted, a PermissionsManager instance is created and used to request location permissions. The result of the permission request is handled in the onPermissionResult method.
Tracking Current Location Automatically when Map Ready:
-
The onMapReady method is called when the map is ready.
-
The nextbillionMap instance is assigned.
-
A CameraPosition is created to set the initial camera position of the map.
-
The map style is set using a Style.Builder and the activateLocationComponent method are called to activate the location component.
Add CameraTracking Change Listener to Update Tracking Status:
-
The activateLocationComponent method is called when the location component is activated.
-
The OnCameraTrackingChangedListener is added to the location component to listen for changes in camera tracking status.
-
When tracking is dismissed, the tracking button's image is updated.
-
The addOnLocationClickListener and addOnLocationLongClickListener methods are used to listen for location clicks and long clicks.
Click Location Tracking Button to Track Current Location:
-
The trackLocation button's click listener is implemented.
-
When clicked, the button image is updated, and the trackLocation method is called.
-
In the trackLocation method, the location component is retrieved from the nextbillionMap and configured.
-
The location component's style is updated to enable pulsing.
-
The location component is enabled, and the CameraMode is set to TRACKING.
Additional notes:
-
The code includes lifecycle methods (onStart, onResume, onPause, onStop, onSaveInstanceState, onDestroy, onLowMemory) to manage the lifecycle of the MapView.
-
The code uses Nextbillion map library classes and methods for map-related operations.
-
The code includes resource IDs for icons and handles their display and click events.
-
The code demonstrates how to handle location permissions using the PermissionsManager class.