• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

MapView Markers

This example shows how to Add markers in bulk

  • Add markers in bulk to MapView

  • Click MapView to Add a marker with clicked point

    • position(point)
    • title(title)
    • snippet(snippet);
MapView Markers

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

activity_marker.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 tools:context=".MainActivity">
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.550813508267716"
15 app:nbmap_cameraTargetLng="9.992248999933745"
16 app:nbmap_cameraZoom="12" />
17
18

MarkersActivity view source

1package ai.nextbillion;
2
3import android.app.ProgressDialog;
4import android.graphics.PointF;
5import android.os.AsyncTask;
6import android.os.Bundle;
7import android.view.Menu;
8import android.view.MenuItem;
9
10import java.io.IOException;
11import java.lang.ref.WeakReference;
12import java.text.DecimalFormat;
13import java.util.ArrayList;
14import java.util.List;
15import java.util.Random;
16
17import ai.nextbillion.maps.annotations.MarkerOptions;
18import ai.nextbillion.maps.core.MapView;

The given code is an Android activity that demonstrates different functionalities related to working with markers on a map using the Nextbillion Maps SDK. The activity allows users to add markers in bulk, add markers by clicking on the map, and reset the map.

initMapView:

  • The onCreate method initializes the MapView by setting the content view and obtaining a reference to it using findViewById. Then, the mapView.onCreate(savedInstanceState) method is called to initialize the MapView and pass the saved instance state.

add markers in bulk to MapView:

  • The showGlMarkers method is responsible for adding markers in bulk to the NextbillionMap.

  • Generates random indexes and retrieves corresponding LatLng coordinates from the locations list.

  • Creates a MarkerOptions object for each coordinate, including a title and snippet, and adds them to the markerOptionsList.

  • Finally, the nextbillionMap.addMarkers(markerOptionsList) method is called to add all the markers to the map.

click MapView to add a marker:

  • The addOnMapLongClickListener and addOnMapClickListener methods are used to listen for long click and click events on the map, respectively. When a long click or click occurs, the addMarker method is called with the clicked LatLng point.

  • The method converts the LatLng to a PointF using nextbillionMap.getProjection().toScreenLocation(point).

  • It creates a MarkerOptions object with the position, title, and snippet based on the clicked point and adds it to both the markerOptionsList and the nextbillionMap using nextbillionMap.addMarker(marker).

Overall, the code demonstrates how to initialize the MapView, add markers in bulk to the NextbillionMap, and add markers by clicking on the map.

Have Questions ?