Center Camera In Bounds
This example shows how to Center Camera in Bounds
-
Create polygon with custom bound area
-
Center Camera to given bounds and padding
-
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 150))

For all code examples, refer to Android Maps SDK Code Examples
activity_bounds.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 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 tools:context=".PolygonActivity">
8
9 <ai.nextbillion.maps.core.MapView
10 android:id="@+id/map_view"
11 android:layout_width="match_parent"
12 android:layout_height="match_parent"
13 app:nbmap_uiAttribution="false"
14 app:nbmap_cameraTargetLat="53.5511"
15 app:nbmap_cameraTargetLng="9.9937"
16 app:nbmap_cameraZoom="12.5" />
17
18 <ImageView
19 android:id="@+id/iv_back"
20 android:layout_width="40dp"
21 android:layout_height="40dp"
22 android:layout_marginLeft="16dp"
23 app:layout_constraintTop_toTopOf="parent"
24 app:layout_constraintLeft_toLeftOf="parent"
25 android:layout_marginTop="16dp"
26 android:background="@drawable/circle_white_bg"
27 android:src="@drawable/icon_back"
28 app:tint="@color/color_back_icon"/>
29
30 <com.google.android.material.floatingactionbutton.FloatingActionButton
31 android:id="@+id/removeBounds"
32 android:layout_width="wrap_content"
33 android:layout_height="wrap_content"
34 android:layout_gravity="right|bottom"
35 android:layout_marginRight="20dp"
36 android:layout_marginBottom="40dp"
37 android:visibility="gone"
38 app:elevation="10dp"
39 app:backgroundTint="@color/color_4158ce"
40 app:pressedTranslationZ="10dp"
41 app:fabSize="normal"
42 android:src="@drawable/icon_close"/>
43</androidx.coordinatorlayout.widget.CoordinatorLayout>
BoundsActivity view source
1package ai.nextbillion;
2import ai.nextbillion.databinding.ActivityBoundsBinding;
3import ai.nextbillion.maps.annotations.PolygonOptions;
4import ai.nextbillion.maps.camera.CameraUpdateFactory;
5import ai.nextbillion.maps.core.NextbillionMap;
6import ai.nextbillion.maps.core.Style;
7import ai.nextbillion.maps.geometry.LatLng;
8import ai.nextbillion.maps.geometry.LatLngBounds;
9import android.graphics.Color;
10import android.os.Bundle;
11import android.view.LayoutInflater;
12import androidx.appcompat.app.AppCompatActivity;
13
14
15/**
16 * @author qiuyu
17 * @Date 2022/6/30
18 **/
19class BoundsActivity : AppCompatActivity(){
20
21 private lateinit var binding: ActivityBoundsBinding
22 private lateinit var mMap: NextbillionMap
23 private var boundsPoints: MutableList<LatLng> = mutableListOf()
24 private var boundsArea: PolygonOptions? = null
25
26 override fun onCreate(savedInstanceState: Bundle?) {
27 super.onCreate(savedInstanceState)
28 binding = ActivityBoundsBinding.inflate(LayoutInflater.from(this))
29 setContentView(binding.root)
30 binding.mapView.onCreate(savedInstanceState)
31 binding.mapView.getMapAsync { map: NextbillionMap -> onMapSync(map) }
32 binding.removeBounds.setOnClickListener {
33 removeBounds()
34 }
35 binding.ivBack.setOnClickListener {
36 finish()
37 }
38 }
39
40 private fun removeBounds() {
41 mMap.clear()
42 boundsPoints.clear()
43 boundsArea = null
44 binding.removeBounds.hide()
45 }
46
47 private fun onMapSync(map: NextbillionMap) {
48 mMap = map
49 mMap.addOnMapLongClickListener {
50 checkBounds(it)
51 false
52 }
53 map.setStyle( Style.Builder().fromUri(StyleConstants.LIGHT))
54 }
55
56 private fun checkBounds(latLng: LatLng) {
57 mMap.addMarker(latLng)
58 boundsPoints.add(latLng)
59
60 if (boundsPoints.size >= 4) {
61 binding.removeBounds.show()
62 boundsArea?.let {
63 mMap.removePolygon(it.polygon)
64 }
65 val bounds = LatLngBounds.Builder().includes(boundsPoints).build()
66 mMap.setLatLngBoundsForCameraTarget(bounds);
67 boundsArea = PolygonOptions()
68 .add(bounds.northWest)
69 .add(bounds.northEast)
70 .add(bounds.southEast)
71 .add(bounds.southWest)
72 boundsArea?.let {
73 it.alpha(0.25f)
74 it.fillColor(Color.RED)
75 mMap.addPolygon(it)
76 }
77 mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 150))
78 }
79
80 }
81
82 ///////////////////////////////////////////////////////////////////////////
83 // Lifecycle
84 ///////////////////////////////////////////////////////////////////////////
85 override fun onStart() {
86 super.onStart()
87 binding.mapView.onStart()
88 }
89
90 override fun onResume() {
91 super.onResume()
92 binding.mapView.onResume()
93 }
94
95 override fun onPause() {
96 super.onPause()
97 binding.mapView.onPause()
98 }
99
100 override fun onStop() {
101 super.onStop()
102 binding.mapView.onStop()
103 }
104
105 override fun onSaveInstanceState(outState: Bundle) {
106 super.onSaveInstanceState(outState)
107 binding.mapView.onSaveInstanceState(outState)
108 }
109
110 override fun onDestroy() {
111 super.onDestroy()
112 binding.mapView.onDestroy()
113 }
114
115 override fun onLowMemory() {
116 super.onLowMemory()
117 binding.mapView.onLowMemory()
118 }
119}
The given code is an Android activity that allows users to create a polygon by selecting points on a map. It also provides the functionality to remove the created polygon.
Initializing the MapView:
-
The MapView is initialized in the onCreate method using binding.mapView.onCreate(savedInstanceState).
-
The map is obtained asynchronously using binding.mapView.getMapAsync and the onMapSync method is called.
Creating a Polygon with Custom Bound Area:
-
The onMapSync method sets up the NextbillionMap and adds a map long click listener.
-
When a long click event occurs, the checkBounds method is called with the clicked LatLng coordinate.
-
The clicked LatLng is added to the boundsPoints list, and a marker is placed at that location on the map.
-
If there are at least 4 points in the boundsPoints list, a polygon is created with the bounds using LatLngBounds.Builder().includes(boundsPoints).build().
-
The existing bounds area is removed from the map using mMap.removePolygon(it.polygon).
-
A new PolygonOptions object is created and added to the map with the appropriate points and styling.
-
The camera is animated to center on the created bounds using mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 150)).
Centering Camera to Given Bounds and Padding:
-
The mMap.setLatLngBoundsForCameraTarget(bounds) method is used to set the bounds for the camera movement.
-
The camera is animated to center on the created bounds with a padding of 150 pixels.
Removing Bounds:
-
The removeBounds method is called when the "removeBounds" button is clicked.
-
It clears the map, clears the boundsPoints list, sets boundsArea to null, and hides the "removeBounds" button. Lifecycle Methods:
The code also includes various lifecycle methods (onStart, onResume, onPause, onStop, onSaveInstanceState, onDestroy, onLowMemory) that should be implemented when using the MapView to properly manage its lifecycle and handle configuration changes.
Note: The code uses the Nextbillion Maps SDK, which provides map-related functionalities. It also utilizes the LatLng class to represent geographical coordinates and the LatLngBounds class to define a rectangular area on the map.