• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

MapView CameraPosition Method

This example shows CameraPosition Method on MapView

  • CameraPosition Method

    • target

    • zoom

    • bearing

    • tilt

MapView CameraPosition Method

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

activity_camera_position.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
8 <ai.nextbillion.maps.core.MapView
9 android:id="@id/mapView"
10 android:layout_width="match_parent"
11 android:layout_height="match_parent"
12 app:nbmap_uiAttributionTintColor="@color/redAccent" />
13
14 <com.google.android.material.floatingactionbutton.FloatingActionButton
15 android:id="@+id/fab"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:layout_gravity="end|bottom"

CameraPositionActivity view source

1package ai.nextbillion;
2
3import android.content.Context;
4import android.content.DialogInterface;
5import android.os.Bundle;
6import android.view.LayoutInflater;
7import android.view.View;
8import android.widget.SeekBar;
9import android.widget.TextView;
10import android.widget.Toast;
11
12import com.google.android.material.floatingactionbutton.FloatingActionButton;
13
14import ai.nextbillion.maps.camera.CameraPosition;
15import ai.nextbillion.maps.camera.CameraUpdateFactory;
16import ai.nextbillion.maps.core.MapView;
17import ai.nextbillion.maps.core.NextbillionMap;
18import ai.nextbillion.maps.core.OnMapReadyCallback;

The code is an activity called CameraPositionActivity that demonstrates how to listen to camera change events in a map view. It uses the Nextbillion Maps SDK for Android.

initMapView:

  • The map view is initialized in the onCreate method by setting the content view to the layout file activity_camera_position.xml. The map view is found using findViewById and the getMapAsync method is called to set the callback for when the map is ready.

Operations on Camera Position:

  • The onMapReady method is implemented to receive the NextbillionMap object when it is ready. The map style is set to StyleConstants.SATELLITE_STREETS, and a listener is added to a floating action button (FAB).

  • The toggleLogCameraChanges method is used to toggle the logging of camera changes. When logging is enabled, listeners for camera events are added to the map. When logging is disabled, the listeners are removed.

  • The onMapLongClick method is implemented to toggle the logging of camera changes when a long click event occurs on the map.

  • The onClick method is implemented to show a dialog when the FAB is clicked. The dialog allows the user to set the camera position parameters (latitude, longitude, zoom, bearing, and tilt) and animates the camera to the new position when the "Animate" button is clicked.

  • The LatLngChangeListener class extends ValueChangeListener and adjusts the latitude value by subtracting 180, as the seek bar range is from 0 to 360 but latitude ranges from -90 to 90.

  • The DialogClickListener class is an implementation of DialogInterface.OnClickListener that retrieves the camera position parameters from the dialog content and animates the camera to the new position using nextbillionMap.animateCamera.

Additionally, the code includes lifecycle methods (onStart, onResume, onPause, onStop, onDestroy, onSaveInstanceState) to manage the state of the map view and release resources properly.

The activity demonstrates how to initialize a map view, handle camera change events, and interact with the camera position using a dialog.

Have Questions ?