• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Location Tracking Modes

This example shows how to Switch Location Tracking Modes

  • Location Permissions Handling

  • Switch Location component RenderMode

  • Switch Location Tracking Camera Mode

  • Enable/Disable Location Component

  • Tracking Current Location Automatically when MapReady

    • locationComponent.setLocationComponentEnabled(true);
    • locationComponent.setRenderMode(RenderMode.COMPASS);
    • locationComponent.setCameraMode(CameraMode.TRACKING);
Location Tracking Modes

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

activity_location_modes.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
8 <ai.nextbillion.maps.core.MapView
9 android:id="@+id/mapView"
10 android:layout_width="match_parent"
11 android:layout_height="match_parent"
12 android:layout_marginBottom="0dp"
13 app:layout_constraintBottom_toTopOf="parent"
14 app:layout_constraintLeft_toLeftOf="parent"
15 app:layout_constraintRight_toRightOf="parent"
16 app:layout_constraintTop_toTopOf="parent"
17 app:nbmap_uiAttribution="false" />
18

LocationModesActivity view source

1package ai.nextbillion;
2
3import android.annotation.SuppressLint;
4import android.graphics.RectF;
5import android.location.Location;
6import android.os.Bundle;
7import android.view.Menu;
8import android.view.MenuItem;
9import android.view.View;
10import android.widget.ArrayAdapter;
11import android.widget.Button;
12import android.widget.Toast;
13
14import java.util.ArrayList;
15import java.util.List;
16
17import ai.nextbillion.maps.camera.CameraUpdateFactory;
18import ai.nextbillion.maps.core.MapView;

Summary: The given code is an Android activity that demonstrates various location modes and features using the NextbillionMap library. It initializes a map view, handles location permissions, tracks the current location, switches between location component render modes and tracking camera modes and enables or disables the location component.

initMapView:

  • The map view is initialized in the onCreate method by finding the view with the ID R.id.mapView. This method sets up the map view and initializes other UI elements and variables.

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 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 MapReady:

  • When the map is ready (onMapReady), the NextbillionMap instance is obtained. The map's style is set, and the location component is activated with various options, including a specialized location layer, default location engine, and location update interval. The location component is enabled and configured with listeners for location clicks and camera tracking changes. The last known location is also updated.

Switch Location component RenderMode:

  • The render mode can be switched using the showModeListDialog method, which displays a dialog with options for Normal, Compass, and GPS render modes. The selected mode is applied to the location component using setRendererMode().

Switch Location Tracking CameraMode:

  • The camera tracking mode can be switched using the showTrackingListDialog method, which displays a dialog with options for different camera tracking modes. The selected mode is applied to the location component using setCameraTrackingMode().

Enable/Disable Location Component:

  • The location component can be enabled or disabled by clicking on the corresponding menu items in the options menu. The status of the location component is updated accordingly using setLocationComponentEnabled.

The code also includes various lifecycle methods for managing the map view, saving and restoring the state of the activity, and handling user interactions with the location component, such as clicking and dismissing camera tracking.

Overall, the code demonstrates how to use the NextbillionMap library to implement location-based features in an Android application, including handling permissions, tracking the user's location, and customizing the render and camera tracking modes of the location component.

Have Questions ?