Open In App

MapmyIndia Android SDK – Add Custom Marker

Improve
Improve
Like Article
Like
Save
Share
Report

Many android applications use maps within their application. We will get to see that they display custom marker icons within their maps. In this article we, will take a look at How to add a custom marker to Map my India Maps in our 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: Create a layout file for displaying a tooltip on the marker

Navigate to app>res>layout>Right click on it>New>Layout Resource file and name it as tooltip and add the below code to it. Comments are added in the code to get to know it in detail. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
  
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        android:padding="5dp">
  
        <!-- displaying title on below line-->
        <TextView
            android:id="@+id/tooltip_title"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:gravity="center"
            android:maxEms="17"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
  
        <!--displaying description on below line-->
        <TextView
            android:id="@+id/tooltip_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:maxEms="17"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            android:visibility="gone" />
  
        <!--displaying sub description on below line-->
        <TextView
            android:id="@+id/tooltip_sub_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:maxEms="17"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textSize="10sp" />
  
    </LinearLayout>
  
    <!--displaying tip view on below line-->
    <com.mmi.view.TipView
        android:id="@+id/tip_view"
        android:layout_width="fill_parent"
        android:layout_height="10dp" />
  
</LinearLayout>


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.BasicInfoWindow
import com.mmi.layers.Marker
import com.mmi.util.GeoPoint
  
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 adding latitude and longitude
        val geoPoint = GeoPoint(28.7041, 77.1025)
        // on below line we are creating an info window for our tool tip.
        val infoWindow = BasicInfoWindow(R.layout.tooltip, mapView)
        // on below line setting tooltip color.
        infoWindow.setTipColor(resources.getColor(R.color.white))
        // on below line we are creating our marker.
        val marker = Marker(mapView)
        // on below line setting title,description
        // and custom icon for marker.
        marker.title = "New Delhi"
        marker.description = "Capital of India"
        marker.icon = resources.getDrawable(R.drawable.delhi)
        // on below line we are setting marker position.
        marker.position = geoPoint
        // on below line we are adding anchor for our marker.
        marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER)
        // on below line we are adding our marker to map view.
        mapView.overlays.add(marker)
        // on below line we are setting map 
        // view center position to marker.
        mapView.invalidate()
        mapView.setCenter(geoPoint)
    }
}


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.BasicInfoWindow;
import com.mmi.layers.Marker;
import com.mmi.util.GeoPoint;
  
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 adding latitude and longitude
        GeoPoint geoPoint = new GeoPoint(28.7041, 77.1025);
        // on below line we are creating an info window for our tool tip.
        BasicInfoWindow infoWindow = new BasicInfoWindow(R.layout.tooltip, mapView);
        // on below line setting tooltip color.
        infoWindow.setTipColor(getResources().getColor(R.color.white));
        // on below line we are creating our marker.
        Marker marker = new Marker(mapView);
        // on below line setting title,description 
        // and custom icon for marker.
        marker.setTitle("New Delhi");
        marker.setDescription("Capital of India");
        marker.setIcon(getResources().getDrawable(R.drawable.delhi));
        // on below line we are setting marker position.
        marker.setPosition(geoPoint);
        // on below line we are adding anchor for our marker.
        marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER);
        // on below line we are adding our marker to map view.
        mapView.getOverlays().add(marker);
        // on below line we are setting map view
        // center position to marker.
        mapView.invalidate();
        mapView.setCenter(geoPoint);
  
    }
}


Note: Make sure to add the image which you want to display in the drawable folder.

Now run your application to see the output of it. 

Output:



Last Updated : 15 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads