MapmyIndia Android SDK – Add Polygon to Map
Many times in the android application which displays maps within their android application we will get to see the maps and also display polygons on the maps. In this article, we will take a look at How to add polylines on the map using Map My India SDK in Android.
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
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity" >
< 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: 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.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mmi.LicenceManager
import com.mmi.MapmyIndiaMapView
import com.mmi.layers.Polygon
import com.mmi.util.GeoPoint
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
LicenceManager.getInstance().restAPIKey = "Enter your API key"
LicenceManager.getInstance().mapSDKKey = "Enter your maps sdk key"
val mapmyIndiaMapView = findViewById<MapmyIndiaMapView>(R.id.idMapView)
val mapView = mapmyIndiaMapView.mapView
val geoPoints = ArrayList<GeoPoint>()
geoPoints.add(GeoPoint( 28.549356 , 77.26780099999999 ))
geoPoints.add(GeoPoint( 28.551844 , 77.26749 ))
geoPoints.add(GeoPoint( 28.554454 , 77.265473 ))
geoPoints.add(GeoPoint( 28.549637999999998 , 77.262909 ))
val polygon: Polygon = Polygon( this )
polygon.points = geoPoints
polygon.fillColor = Color.GREEN
mapView.setBounds(geoPoints)
mapView.getOverlays().add(polygon)
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.Polygon;
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);
LicenceManager.getInstance().setRestAPIKey( "Enter your API key" );
LicenceManager.getInstance().setMapSDKKey( "ENter your maps sdk key" );
MapmyIndiaMapView mapmyIndiaMapView = findViewById(R.id.idMapView);
MapView mapView = mapmyIndiaMapView.getMapView();
ArrayList<GeoPoint> geoPoints = new ArrayList<>();
geoPoints.add( new GeoPoint( 28.549356 , 77.26780099999999 ));
geoPoints.add( new GeoPoint( 28.551844 , 77.26749 ));
geoPoints.add( new GeoPoint( 28.554454 , 77.265473 ));
geoPoints.add( new GeoPoint( 28.549637999999998 , 77.262909 ));
Polygon polygon = new Polygon(MainActivity. this );
polygon.setPoints(geoPoints);
polygon.setFillColor(getResources().getColor(R.color.purple_200));
mapView.setBounds(geoPoints);
mapView.getOverlays().add(polygon);
mapView.invalidate();
}
}
|
Now run your application to see the output of it.
Output:
Last Updated :
16 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...