Open In App

MapmyIndia Android SDK – Add Marker Clustering

Improve
Improve
Like Article
Like
Save
Share
Report

Many times in android applications we get to see we have to display multiple markers. If the markers displayed within the maps are close to each other then we get to see that the markers overlap each other. In that case, many applications display the number of markers present at that location instead of displaying the marker. This method of displaying the number of markers is called marker clustering. In this article, we will take a look at How to add marker clustering on Map My India Maps in the Android application. A sample video is given below to get an idea about what we are going to do in this article.

Note: This Android article covered in both Java and Kotlin languages. 

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Downloading SDK for Map My India

Navigate to the link and download the jar file for Map My India SDK. After downloading it copy the file to add it to our project. Navigate to the Project section in your android studio project. Then navigate to Project Name>app>libs. Right-click on it and simply paste the downloaded jar file into that folder.

Step 3: Adding dependency in build.gradle file

Navigate to app>build.gradle file and add the below dependency to it in the dependencies section.

implementation 'com.google.code.gson:gson:2.3'
implementation files('libs/map_sdk_2.1.jar')

After adding the dependency simply sync your project.

Step 4: Working with the activity_main.xml file

Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <!--on below line we are creating a map view-->
    <com.mmi.MapmyIndiaMapView
        android:id="@+id/idMapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
</RelativeLayout>


Step 5: Adding permissions in the AndroidManifest.xml file

Navigate to app>AndroidManifest.xml file and add the below permissions to it.  

XML




<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Now add the below code in the application tag in the AndroidManifest.xml file to use the legacy library within our application.

XML




<uses-library
  android:name="org.apache.http.legacy"
  android:required="false" />


Step 6: Generating your API key for using Map My India SDK

Navigate to the link. Sign Up with your email and password and you will get to see the below screen. Inside that navigate to the credentials tab to see your API key. Copy that API key that we have to use in our project.

 

Step 7: Creating a Modal class for Marker Model

Navigate to app>java>your app’s package name>Right click on it>New>Java/Kotlin class and name it as MarkerModel and add the below code to it. Comments are added in the code to get to know it in detail.  

Kotlin




package com.gtappdevelopers.mapmyindiakotlin
 
import com.mmi.util.GeoPoint
 
data class MarkerModel(
    // on below line creating variable
    // for title and geo point
    val title: String,
    val geoPoint: GeoPoint
);


Java




package com.gtappdevelopers.mapmyindia;
 
import com.mmi.util.GeoPoint;
 
public class MarkerModel {
    // creating variables on below line.
    private String title;
    private GeoPoint geoPoint;
 
    public MarkerModel(String title, String imageUrl,
                       String description,
                       String subDescription,
                       GeoPoint geoPoint)
    {
        this.title = title;
 
        this.geoPoint = geoPoint;
    }
 
    public String getTitle() { return title; }
 
    public void setTitle(String title)
    {
        this.title = title;
    }
 
    public MarkerModel(String title, GeoPoint geoPoint)
    {
        this.title = title;
        this.geoPoint = geoPoint;
    }
 
    public GeoPoint getGeoPoint() { return geoPoint; }
 
    public void setGeoPoint(GeoPoint geoPoint)
    {
        this.geoPoint = geoPoint;
    }
}


Step 8: Working with the MainActivity file 

Navigate to app > java > your app’s package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.mapmyindiakotlin
 
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mmi.LicenceManager
import com.mmi.MapmyIndiaMapView
import com.mmi.layers.Marker
import com.mmi.layers.MarkerClusterer
import com.mmi.util.GeoPoint
import java.util.*
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // on below line we are setting rest api key and map sdk key.
        LicenceManager.getInstance().restAPIKey = "Enter your API key"
        LicenceManager.getInstance().mapSDKKey = "Enter your API key"
        // on below line we are creating variables for map view and initializing it.
        val mapmyIndiaMapView = findViewById<MapmyIndiaMapView>(R.id.idMapView)
        // on below line we are getting our map view.
        val mapView = mapmyIndiaMapView.mapView
        // on below line we are creating an array list to display all markers.
        val markerModels: ArrayList<MarkerModel> = ArrayList<MarkerModel>()
        // on below line adding marker title and geo point to it.
        markerModels.add(MarkerModel("Location 1", GeoPoint(28.549356, 77.26780099999999)))
        markerModels.add(MarkerModel("Location 2", GeoPoint(28.551844, 77.26749)))
        markerModels.add(MarkerModel("Location 3", GeoPoint(28.554454, 77.265473)))
        markerModels.add(MarkerModel("Location 4", GeoPoint(28.549637999999998, 77.262909)))
        markerModels.add(MarkerModel("Location 5", GeoPoint(28.555245, 77.266117)))
        markerModels.add(MarkerModel("Location 6", GeoPoint(28.558149, 77.269787)))
        // on below line creating a marker clusterer
        val markerClusterer = MarkerClusterer(this)
        // on below line setting color for marker cluster.
        markerClusterer.setColor(resources.getColor(R.color.purple_200))
        // on below line setting anchor text for marker clusterer
        markerClusterer.mAnchorV = Marker.ANCHOR_CENTER
        markerClusterer.mTextAnchorV = Marker.ANCHOR_CENTER
        // on below line setting text size.
        markerClusterer.setTextSize(12)
        // on below line creating a list for geo points
        val points = ArrayList<GeoPoint>()
        // on below line running a loop for setting markers.
        for (markerModel in markerModels) {
            // on below line we are creating a marker.
            val marker = Marker(mapView)
            // on below line setting title
            // and position for marker.
            marker.title = markerModel.title
            marker.position = markerModel.geoPoint
            // on below line adding related
            // object for marker model.
            marker.relatedObject = markerModel
            markerClusterer.add(marker)
            points.add(markerModel.geoPoint)
        }
        // on below line we are setting
        // bounds for map view.
        mapView.setBounds(points)
        // on below line adding marker cluster.
        mapView.overlays.add(markerClusterer)
        mapView.invalidate()
    }
}


Java




package com.gtappdevelopers.mapmyindia;
 
import android.os.Bundle;
 
import androidx.appcompat.app.AppCompatActivity;
 
import com.mmi.LicenceManager;
import com.mmi.MapView;
import com.mmi.MapmyIndiaMapView;
import com.mmi.layers.Marker;
import com.mmi.layers.MarkerClusterer;
import com.mmi.util.GeoPoint;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // on below line we are setting rest api key and map sdk key.
        LicenceManager.getInstance().setRestAPIKey("Enter your API key");
        LicenceManager.getInstance().setMapSDKKey("Enter your API key");
        // on below line we are creating variables for map view and initializing it.
        MapmyIndiaMapView mapmyIndiaMapView = findViewById(R.id.idMapView);
        // on below line we are getting our map view.
        MapView mapView = mapmyIndiaMapView.getMapView();
        // on below line we are creating an array list to display all markers.
        ArrayList<MarkerModel> markerModels = new ArrayList<>();
        // on below line adding marker title and geo point to it.
        markerModels.add(new MarkerModel("Location 1", new GeoPoint(28.549356, 77.26780099999999)));
        markerModels.add(new MarkerModel("Location 2", new GeoPoint(28.551844, 77.26749)));
        markerModels.add(new MarkerModel("Location 3", new GeoPoint(28.554454, 77.265473)));
        markerModels.add(new MarkerModel("Location 4", new GeoPoint(28.549637999999998, 77.262909)));
        markerModels.add(new MarkerModel("Location 5", new GeoPoint(28.555245, 77.266117)));
        markerModels.add(new MarkerModel("Location 6", new GeoPoint(28.558149, 77.269787)));
        // on below line creating a marker clusterer
        MarkerClusterer markerClusterer = new MarkerClusterer(this);
        // on below line setting color for marker cluster.
        markerClusterer.setColor(getResources().getColor(R.color.purple_200));
        // on below line setting anchor text for marker clusterer
        markerClusterer.mAnchorV = Marker.ANCHOR_CENTER;
        markerClusterer.mTextAnchorV = Marker.ANCHOR_CENTER;
        // on below line setting text size.
        markerClusterer.setTextSize(12);
        // on below line creating a list for geo points
        ArrayList<GeoPoint> points = new ArrayList<>();
        // on below line running a loop for setting markers.
        for (MarkerModel markerModel : markerModels) {
            // on below line we are creating a marker.
            Marker marker = new Marker(mapView);
            // on below line setting title and position for marker.
            marker.setTitle(markerModel.getTitle());
            marker.setPosition(markerModel.getGeoPoint());
            // on below line adding related object for marker model.
            marker.setRelatedObject(markerModel);
            markerClusterer.add(marker);
            points.add(markerModel.getGeoPoint());
        }
        // on below line we are setting bounds for map view.
        mapView.setBounds(points);
        // on below line adding marker cluster.
        mapView.getOverlays().add(markerClusterer);
        mapView.invalidate();
    }
}


Now run your application to see the output of it. 

Output:



Last Updated : 23 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads