android SDKs

Vector-based map data, provides mapping capabilities with features such as routing, navigation, historical traffic patterns

MapmyIndia Vector Map Android SDK

Getting Started

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.

API Usage

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 plans 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.

Setup your project

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'(New)
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'(New)

 
  • Add these permissions in your project
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
<uses-permission android:name="android.permission.INTERNET" >
Add your API keys to the SDK(in your application.s on Create 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());
You cannot use the MapmyIndia Map Mobile SDK without these function calls. You will find your keys in your API Dashboard.

Add a MapmyIndia Map to your application

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent" 
android:layout_height="match_parent"
/> 

NOTE: All the lifecycle methods that need to be overridden:

onCreate();
onStart();
onResume();
onPause();
onStop();
onSaveInstanceState();
onLowMemory();
onDestroy();

Map Interactions

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 –

Zoom Controls

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

Target

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

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

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

Zoom controls the scale of the map and consumes any value between 0 and 22. At zoom level 0, the viewport shows continents and other world features. A middle value of 11 will show city level details, and at a higher zoom level, the map will begin to show buildings and points of interest. The camera can zoom in the 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.
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();

We can pass this camera positions to following functions

moveCamera()
easeCamera()
animateCamera()

Map Events

The SDK allows you to listen to certain events on the map. It sets a callback that is invoked when camera movement has started.

mapboxMap.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();
}
});

Sets a callback that is invoked when camera position changes.

mapboxMap.setOnCameraMoveListener(new MapboxMap.OnCameraMoveListener() 
      {
@Override
public void onCameraMove() {
Toast.makeText(MainActivity.this, "onCameraMove", Toast.LENGTH_LONG).show();
        }
});

It sets a callback that is invoked when camera movement was cancelled.

mapboxMap.setOnCameraMoveCancelListener(new MapboxMap.OnCameraMoveCanceledListener()
         { 
@Override
public void onCameraMoveCanceled() {
Toast.makeText(MainActivity.this, "onCameraMoveCanceled", Toast.LENGTH_LONG).show();
         }
});

It sets a callback that is invoked when camera movement has ended.

mapboxMap.setOnCameraIdleListener(new  MapboxMap.OnCameraIdleListener() 
         { 
@Override
public void onCameraIdle() {
Toast.makeText(MainActivity.this, "onCameraIdle", Toast.LENGTH_LONG).show();
         }
});

Map Click/Long Press

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.

mapboxMap.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();
      }
});

Sets a callback that's invoked when the user long clicks on the map view.

mapboxMap.setOnMapLongClickListener(new MapboxMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(@NonNull LatLng point) {
String string = String.format(Locale.US, "User clicked at: %s", point.toString())
Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show();
	        }
});

Map Overlays

Add a marker

Add markers to the map by following these steps –

MarkerOptions  markerOptions =  new MarkerOptions().position(point).icon(icon);
Marker marker1 = map.addMarker(markerOptions);
markerOptions.setTitle("");
markerOptions.setSnippet("");

Remove a Marker

mapboxMap.removeMarker()

Customize a marker

MarkerOptions  markerOptions =  new MarkerOptions().position(point).icon(icon);
Marker marker = mapBoxMap.addMarker(markerOptions);
String tittle = “abc”;
marker.setTitle(tittle);
mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
@Nullable
@Override
public View getInfoWindow(@NonNull Marker marker) {
View view = (context).getLayoutInflater()
.inflate(R.layout.layout, null);
TextView text = (TextView)view.findViewById(R.id.text);
text.setText(marker.getTitle());
return  view;
}
});

Add a Polyline

Draw polyline on the map

mapboxMap.addPolyline(new PolylineOptions()
.addAll(points)//list of LatLng
.color(Color.parseColor("#3bb2d0"))
.width(2));

Add a Polygon

Draw a polygon on the map

mapboxMap.addPolygon(new PolygonOptions()
.addAll(polygon)//list of LatLng
.fillColor(Color.parseColor("#3bb2d0")));

Show user location

Show the current user location

LocationComponentOptions options = LocationComponentOptions.builder(this)
.trackingGesturesManagement(true)
.accuracyColor(ContextCompat.getColor(this, R.color.colorAccent))
.build();
// Get an instance of the component
LocationComponent locationComponent = mapboxMap.getLocationComponent();
// Activate with options
locationComponent.activateLocationComponent(this, 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) {
mapboxMap.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();
}

Proguard

# 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>;
}

Check Demo application for more implementation details

REST APIs

Geocoding

You can read more on MapmyIndia.s Geocoding API here. If we want to Geocode .Delhi.

new MapmyIndiaGeocoding.Builder()
          .setAddress("Delhi")
          .build()
.enqueueCall(new Callback<PlaceResponse>() {
@Override
public void onResponse(Call<PlaceResponse> call, Response<PlaceResponse> response) {
     //handle response
}
@Override
public void onFailure(Call<PlaceResponse> call, Throwable t) {
            t.printStackTrace();
      }
   });
MapmyIndiaGeocoding.Builder<MapmyIndiaGeocoding.Builder<*>>()
        .setAddress("Delhi")
        .build().enqueueCall(object : Callback<PlaceResponse> {
    override fun onResponse(call: Call<PlaceResponse>, response: Response<PlaceResponse>) {      
    }
    override fun onFailure(call: Call<PlaceResponse>, t: Throwable) {
   }
});

Reverse Geocode

You can find address for a point on the map by using the MapmyIndia Maps Reverse Geocoding API. If you are going to reverse Geocode latitude : 28, Longitude :77 call MapmyIndia ReverseGeocode

As follow

new MapmyIndiaReverseGeoCode.Builder()
  .setLocation(28,77)
.build().enqueueCall(new Callback<PlaceResponse>() {
            @Override
public void onResponse(Call<PlaceResponse> call,  Response<PlaceResponse> response) {
//handle response
            }
@Override
public void onFailure(Call<PlaceResponse> call, Throwable t) {
  t.printStackTrace();
            }
     });
MapmyIndiaReverseGeoCode.Builder<MapmyIndiaReverseGeoCode.Builder<*>>()
        .setLocation(latitude, longitude)
        .build().enqueueCall(object : Callback<PlaceResponse> {
    override fun onResponse(call: Call<PlaceResponse>, response: Response<PlaceResponse>) {
        
    }
   override fun onFailure(call: Call<PlaceResponse>, t: Throwable) {
        
     
    }
});

eLoc / Place Details

If you want to get details for an eLoc .mmi000. call functions as

new MapmyIndiaELoc.Builder()
  .setELoc("mmi000")
  .build().enqueueCall(new Callback<PlaceResponse>() {
           @Override
            public void onResponse(Call<PlaceResponse> call,
				    Response response) {
  //handle response
            }
            @Override
           public void onFailure(Call<PlaceResponse> call, Throwable t) {
               t.printStackTrace();
          }
      }); 
MapmyIndiaEloc.Builder<MapmyIndiaNearby.Builder<*>>()
        .setEloc("MMI000")
        .build()
        .enqueueCall(object : Callback<NearbyAtlasResponse> {
			override fun onResponse(call: Call<NearbyAtlasResponse>, 
 			response: Response<NearbyAtlasResponse>) {
            }}
            override fun onFailure(call: Call<NearbyAtlasResponse>, t: Throwable) {
               
            }
     });

Nearby

If you want to get parking near 28,77 you can call MapmyIndia Nearby as follows

new MapmyIndiaNearby.Builder()
  .setItemCount(10)
  .setKeyword("Parking")
  .setLocation(28,77)
  .setExplain(false)
  .setUsername("test")
  .build()
  .enqueueCall(new Callback<NearbyAtlasResponse>() {
  @Override
  public void onResponse(Call<NearbyAtlasResponse> call, Response<NearbyAtlasResponse> response) {
            	//handle response
      }
     @Override
      public void onFailure(Call<NearbyAtlasResponse> call, Throwable t) {
         t.printStackTrace();
    }
  });
MapmyIndiaNearby.Builder<MapmyIndiaNearby.Builder<*>>()
        .setLocation(latitude, longitude)
        .setKeyword("Parking")
        .build()
        .enqueueCall(object : Callback<NearbyAtlasResponse> {
         override fun onResponse(call: Call<NearbyAtlasResponse>, response: Response<NearbyAtlasResponse>)                    {
           }  
            override fun onFailure(call: Call<NearbyAtlasResponse>, t: Throwable) {
               
            }
     });

Auto-Suggest

To get suggestion against given string around any pair of lat lng you can call function as

new MapmyIndiaAutosuggest.Builder<>()
.setBridge(false)
.setLocation(28,77)
.setQuery("mmi000").build().enqueueCall(new Callback<AutoSuggestAtlasResponse>() {
@Override
public void onResponse(Call<AutoSuggestAtlasResponse> call, 									Response<AutoSuggestAtlasResponse> response) {
//handle response
  }
 @Override
 public void onFailure(Call<AutoSuggestAtlasResponse> call, Throwable t) {
       t.printStackTrace();
   }
 });

	MapmyIndiaGeocoding.Builder<MapmyIndiaGeocoding.Builder<*>>()
 .setAddress(geocodeText)
 .build().enqueueCall(object : Callback<PlaceResponse> {
  override fun onResponse(call: Call<PlaceResponse>, response: Response<PlaceResponse>)
 {
       }
   override fun onFailure(call: Call<PlaceResponse>, t: Throwable) {        
     }
  }) 

Routing

To get directions between two points you can call following function

new MapmyIndiaDirectionsLegacy.Builder()
.setOrigin(Position.fromCoordinates(77,28))
.setDestination(Position.fromCoordinates(77.23,28.1231))
.build().enqueueCall(new Callback<LegacyRouteResponse>(){
@Override
public void onResponse(Call<LegacyRouteResponse> call,
  Response<LegacyRouteResponse> response)  {
      			//handle response
    }
			@Override
			public void onFailure(Call<LegacyRouteResponse> call, Throwable t) {
           t.printStackTrace();
     }
  });
MapmyIndiaDirectionsLegacy.Builder<MapmyIndiaDirectionsLegacy.Builder<*>>()
        .setOrigin(Point.fromLngLat(77.202432,28.594475))
.setDestination(Point.fromLngLat(77.1869828.554676))
 .build()
.enqueueCall(object : Callback<LegacyRouteResponse> {
    override fun onResponse(call: Call<LegacyRouteResponse>, 
			response: Response<LegacyRouteResponse>) {       
  }
    override fun onFailure(call: Call<LegacyRouteResponse>, t: Throwable) {        
  }
})

To decode points (pts)

List polyLine = LineString.fromPolyline(response.body().getResults().getTrips().get(0).getPts(),Constants.PRECISION_6).coordinates();
var polyLine = LineString.fromPolyline(response.body()!!.getResults().getTrips().get(0).getPts(), Constants.PRECISION_6).coordinates() as  List ;

Driving Distance Matrix API

To get Distance between points

ArrayList coordinates=new ArrayList<>();

coordinates.add(Point.fromLngLat(77.25,28.9231));
new MapmyIndiaDistanceLegacy.Builder()
.setCenter(Point.fromLngLat(77.23,28.1231))
.setCoordinates(coordinates)
.build().enqueueCall(new Callback<LegacyDistanceResponse>() {
@Override
public void onResponse(Call<LegacyDistanceResponse> call, Response<LegacyDistanceResponse> response) {
       //handle response
  }
@Override
public void onFailure(Call<LegacyDistanceResponse> call, Throwable t) {
    t.printStackTrace();
  }
});
  MapmyIndiaDistanceLegacy.Builder<MapmyIndiaDistanceLegacy.Builder<*>>()
        .setCoordinates(pointList)
        .setCenter(Point.fromLngLat(77.234230, 28.582864))
        .build()
        .enqueueCall(object : Callback<LegacyDistanceResponse> {
      override fun onResponse(call: Call<LegacyDistanceResponse>, response: Response<LegacyDistanceResponse>) {
              }
            override fun onFailure(call: Call<LegacyDistanceResponse>, t: Throwable) {
                
  }
})