Easy To Integrate Maps & Location APIs & SDKs For Web & Mobile Applications
Powered with India's most comprehensive and robust mapping functionalities. Now Available for Srilanka, Nepal, Bhutan and Bangladesh
-
You can get your api key to be used in this document here: https://www.mapmyindia.com/api/signup
-
The sample code is provided to help you understand the basic functionality of MapmyIndia maps & REST APIs working on Android native development platform.
MapmyIndia Maps SDK for Android lets you easily add MapmyIndia Maps and web services to your own Android application. The SDK for Android supports API 14+. You can have a look at the map and features you will get in your own app by using the MapmyIndia Maps SDK for Android.
Through customized tiles, you can add different map layers to your application and add bunch of controls and gestures to enhance map usability thus creating potent map based solutions for your customers. The SDK handles downloading of map tiles and their display along with a bunch of controls and native gestures.
Your MapmyIndia Maps SDK usage needs a set of license keys (get them here) and is governed by the API terms and conditions. As part of the terms and conditions, you cannot remove or hide the MapmyIndia logo and copyright information in your project.
The allowed SDK hits are described on the user dashboard (http://www.mapmyindia.com/api/dashboard) page. Note that your usage is shared between platforms, so the API hits you make from a web application, Android app or an iOS app all add up to your allowed daily limit.
Follow these steps to add the SDK to your project –
- Create a new project in Android Studio
- Import MapmyIndiaGLAndroidSDK_v{version}.aar file in your project.
- Add Following dependencies for the implementation
implementation 'com.jakewharton.timber:timber:4.5.0'
implementation 'com.google.code.gson:gson:2.8.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation 'com.google.android.gms:play-services-location:15.0.1'
implementation 'com.mapbox.mapboxsdk:mapbox-sdk-turf:3.1.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-gestures:0.3.0'
implementation 'com.mapbox.mapboxsdk:mapbox-sdk-geojson:3.3.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-core:0.2.1'
implementation 'com.facebook.soloader:soloader:0.6.1'
- Add these permissions in your project
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
add following lines in your app module's build.gradle
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
Add your API keys to the SDK (in your application's onCreate() or before using map)
MapmyIndiaAccountManager.getInstance().setRestAPIKey(getRestAPIKey());
MapmyIndiaAccountManager.getInstance().setMapSDKKey(getMapSDKKey());
MapmyIndiaAccountManager.getInstance().setAtlasGrantType(getAtlasGrantType());
MapmyIndiaAccountManager.getInstance().setAtlasClientId(getAtlasClientId());
MapmyIndiaAccountManager.getInstance().setAtlasClientSecret(getAtlasClientSecret());
MapmyIndia.getInstance(getApplicationContext());
MapmyIndiaAccountManager.getInstance().restAPIKey = getRestAPIKey()
MapmyIndiaAccountManager.getInstance().mapSDKKey = getMapSDKKey()
MapmyIndiaAccountManager.getInstance().atlasGrantType = getAtlasGrantType()
MapmyIndiaAccountManager.getInstance().atlasClientId = getAtlasClientId()
MapmyIndiaAccountManager.getInstance().atlasClientSecret = getAtlasClientSecret()
You cannot use the MapmyIndia Map Mobile SDK without these function calls. You will find your keys in your API Dashboard.
<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Initialize the mapView
onCreate(){mapview.onCreate()};
onStart(){mapview.onStart()};
onResume(){mapview.onResume()};
onPause(){mapview.onPause()};
onStop(){mapview.onStop()};
onSaveInstanceState(){mapview.onSaveInstanceState()};
onLowMemory(){mapview.onLowMemory()};
onDestroy(){mapview.onDestroy()};
The MapmyIndia Maps Android SDK allows you to define interactions that you can activate on the map to enable gestures and click events. The following interactions are supported –
The map supports the familiar two-finger pinch and zooms to change zoom level as well as double tap to zoom in. Set zoom to 4 for country level display and 18 for house number display. In this SDK the camera position plays an important role
And following operations can be performed using the CameraPosition
The target is single latitude and longitude coordinate that the camera centers it on. Changing the camera's target will move the camera to the inputted coordinates. The target is a LatLng object. The target coordinate is always at the center of the viewport.
Tilt is the camera's angle from the nadir (directly facing the Earth) and uses unit degrees. The camera's minimum (default) tilt is 0 degrees, and the maximum tilt is 60. Tilt levels use six decimal point of precision, which enables you to restrict/set/lock a map's bearing with extreme precision.
The map camera tilt can also adjust by placing two fingertips on the map and moving both fingers up and down in parallel at the same time or
Bearing represents the direction that the camera is pointing in and measured in degrees clockwise from north.
The camera's default bearing is 0 degrees (i.e. "true north") causing the map compass to hide until the camera bearing becomes a non-zero value. Bearing levels use six decimal point precision, which enables you to restrict/set/lock a map's bearing with extreme precision. In addition to programmatically adjusting the camera bearing, the user can place two fingertips on the map and rotate their fingers.
Zoom controls scale of the map and consumes any value between 0 and 22. At zoom level 0, viewport shows continents and other world features. A middle value of 11 will show city level details.At a higher zoom level, map will begin to show buildings and points of interest. Camera can zoom in following ways:
- Pinch motion two fingers to zoom in and out.
- Quickly tap twice on the map with a single finger to zoom in.
- Quickly tap twice on the map with a single finger and hold your finger down on the screen after the second tap.
- Then slide the finger up to zoom out and down to zoom out.
Sdk provides a OnMapReadyCallback, implements this callback and override it's onMapReady() and set the Camera position inside this method
CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(22.8978, 77.3245)) // Sets the new camera position
.zoom(14) // Sets the zoom to level 14
.tilt(45) // Set the camera tilt to 45 degrees
.build();
mapmyIndiaMap.setCameraPosition(position)
mapmyIndiaMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.8978,77.3245),14)
mapmyIndiaMap.easeCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.8978,77.3245),14)
mapmyIndiaMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.8978,77.3245),14)
The SDK allows you to listen to certain events on the map. It sets a callback that is invoked when camera movement has started.
mapmyIndiaMap.setOnCameraMoveStartedListener(new MapboxMap.OnCameraMoveStartedListener() {
private final String[] REASONS = {
"REASON_API_GESTURE",
"REASON_DEVELOPER_ANIMATION",
"REASON_API_ANIMATION"};
@Override
public void onCameraMoveStarted(int reason) {
String string = String.format(Locale.US,
"OnCameraMoveStarted: %s", REASONS[reason - 1]);
Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
}
});
mapmyIndiaMap.setOnCameraMoveCancelListener(new MapboxMap.OnCameraMoveCanceledListener() {
@Override
public void onCameraMoveCanceled() {
Toast.makeText(MainActivity.this, "onCameraMoveCanceled", Toast.LENGTH_LONG).show();
}
});
mapmyIndiaMap.setOnCameraIdleListener(new MapboxMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
Toast.makeText(MainActivity.this, "onCameraIdle", Toast.LENGTH_LONG).show();
}
});
If you want to respond to a user tapping on a point on the map, you can use a MapEventsOverlay which you need to add on the map as an Overlay.
It sets a callback that's invoked when the user clicks on the map view.
mapmyIndiaMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
String string = String.format(Locale.US, "User clicked at: %s", point.toString())
Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
}
});
mapmyIndiaMap.setOnMapLongClickListener(new MapboxMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(@NonNull LatLng point) {
String string = String.format(Locale.US, "User long clicked at: %s", point.toString());
Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
}
});
MarkerOptions markerOptions = new MarkerOptions().position(point).icon(IconFactory.getInstance(SimpleMapActivity.this).fromResource(R.drawable.ic_android));
markerOptions.setTitle("Marker");
markerOptions.setSnippet("This is a Marker");
Marker marker = mapmyIndiaMap.addMarker(markerOptions);
mapmyIndiaMap.removeMarker()
MarkerOptions markerOptions = new MarkerOptions().position(point).icon(IconFactory.getInstance(context).fromResource(R.drawable.ic_android));
Marker marker = mapmyIndiaMap.addMarker(markerOptions);
marker.setTitle("title");
mapmyIndiaMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
@Nullable
@Override public View getInfoWindow(@NonNull Marker marker) {
View view = getLayoutInflater()
.inflate(R.layout.layout, null);
TextView text = view.findViewById(R.id.text);
text.setText(marker.getTitle());
return view;
}
});
mapmyIndiaMap.addPolyline(new PolylineOptions()
.addAll(points)//list of LatLng
.color(Color.parseColor("#3bb2d0"))
.width(2));
mapmyIndiaMap.addPolygon(new PolygonOptions()
.addAll(polygon)//list of LatLng.
.fillColor(Color.parseColor("#3bb2d0")));
Implement LocationEngineListener and override it's method
LocationComponentOptions options = LocationComponentOptions.builder(context)
.trackingGesturesManagement(true)
.accuracyColor(ContextCompat.getColor(this, R.color.colorAccent))
.build();
// Get an instance of the component LocationComponent
locationComponent = mapmyIndiaMap.getLocationComponent();
// Activate with options
locationComponent.activateLocationComponent(context, options);
// Enable to make component visible
locationComponent.setLocationComponentEnabled(true);
locationEngine = locationComponent.getLocationEngine();
locationEngine.addLocationEngineListener(this);
// Set the component's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
locationComponent.setRenderMode(RenderMode.COMPASS);
@Override
public void onConnected() {
locationEngine.requestLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
mapmyIndiaMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
locationEngine.removeLocationEngineListener(this);
}
@Override
protected void onResume() {
super.onResume();
if (locationEngine != null) {
locationEngine.removeLocationEngineListener(this);
locationEngine.addLocationEngineListener(this);
}
}
@Override
protected void onPause() {
super.onPause();
if (locationEngine != null)
locationEngine.removeLocationEngineListener(this);
}
@Override
protected void onStop() {
super.onStop();
if (locationEngine != null) {
locationEngine.removeLocationEngineListener(this);
locationEngine.removeLocationUpdates();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationEngine != null) {
locationEngine.deactivate();
}
}
# Retrofit 2
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes *Annotation*,Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-dontwarn sun.misc.**
-dontwarn okio.**
-dontwarn okhttp3.**
-keep class retrofit.**
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
@retrofit.http.* <methods>;
}
-keep class com.mmi.services.account.** {
<fields>;
<methods>;
}
-keep class com.mmi.services.api.** {
<fields>;
<methods>;
}
-keep class com.mmi.services.utils.** {
<fields>;
<methods>;
}
For information on the REST API module, please read repository wiki here.
For any queries and support, please contact:
Email us at [email protected]
Stack Overflow Ask a question under the mapmyindia-api
Support Need support? contact us!
Blog Read about the latest updates & customer stories
© Copyright 2019. CE Info Systems Pvt. Ltd. All Rights Reserved. | Terms & Conditions mapbox-gl-native copyright (c) 2014-2019 Mapbox.