• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

MapView Polygon

This example shows how to add Polygons in MapView

  • Add Polygon from a set of Latlng

  • Query Visible Polygon in Rect

  • Set Polygon stroke

  • Set Polygon color

MapView Polygon

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

activity_polygon.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">
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_cameraTargetLat="53.5493093866953"
14 app:nbmap_cameraTargetLng="10.031835837897463"
15 app:nbmap_cameraZoom="10"
16 app:nbmap_uiAttribution="false" />
18 <androidx.core.widget.NestedScrollView

PolygonActivity view source

1public class PolygonActivity extends AppCompatActivity implements View.OnClickListener {
3 private NextbillionMap mMap;
4 private MapView mapView;
5 private BottomSheetBehavior bottomSheetBehavior;
6 private Polygon polygon;
7 // for stroke
8 private Polyline polyline;
10 // polygon
11 private List<LatLng> polygonPoints = new ArrayList<>();
12 private List<Marker> polygonMarkers = new ArrayList<>();
13 private LatLngBounds latLngBounds;
14 private Marker clickMarker;
16 // menu
17 private ColorSelectorView polygonColorSelectorView;
18 private ColorSelectorView polygonStrokeColorSelectorView;


  • Initialises the data for the polygon activity.

  • Sets up various UI components such as color selectors, slider bars, and switch views. It also defines listeners for these components to handle user interactions and update the corresponding data values.


  • Adds listeners to the map.

  • Sets up event listeners for various map interactions such as long clicks, clicks, moves, and marker clicks. These listeners perform actions such as adding markers, creating polygons, checking polygon visibility, and removing markers and polygons.


  • Checks if the polygon is currently visible on the map. It relies on the map's projection and the bounds of the polygon to determine if the polygon is within the visible region of the map. It returns a boolean value indicating the visibility status of the polygon.


  • Creates a polygon on the map. It checks if the necessary data, such as the map and polygon points, are available. It removes any existing polyline and polygon from the map. It updates the marker show style based on the switch state. Then it adds a polygon and a polyline to the map using the provided polygon points, colors, and stroke width.


  • Removes the polygon and related markers from the map. It checks if the map and polygon are available and removes them from the map. It also clears the polygon points, marker list, and polyline if they exist.

These methods are part of the PolygonActivity class and are used to initialize and manage the polygon functionality in the corresponding Android activity.

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

Have Questions ?