Skip to content
Ayush Dayal edited this page Jan 31, 2020 · 17 revisions

MapmyIndia APIs

MapmyIndia Vector Maps Sample App (with SDK - Native) for iOS

Welcome to the mapmyindia-maps-vectorSDK-iOS-sample-withREST-beta wiki!

In this section you will find documentation for :-

Rest API Kit is a iOS wrapper library on MapmyIndia's offered Rest APIs. For more detail go to MapmyIndia's Rest APIs.

MapmyIndia Vector Maps SDK for IOS lets you easily add MapmyIndia Maps and web services to your own iOS app. MapmyIndia Maps SDK for iOS supports iOS SDK 9.0 and above and Xcode 10.1 or later. You can have a look at the map and features you will get in your own app by using the MapmyIndia Maps app for iOS. The SDK handles map tiles download and their display along with a bunch of controls and native gestures.

Your MapmyIndia Vector 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. Please see branding guidelines on MapmyIndia website for more details. 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.

Create a new project in Xcode.

  • Drag and drop the MapmyIndia Map SDK Framework (Mapbox.framework) to your project. It must be added in embedded binaries.
  • Drag and drop the MapmyIndiaAPIKit Framework to your project. It must be added in embedded binaries. It is a dependent framework.
  • In the Build Phases tab of the project editor, click the + button at the top and select .New Run Script Phase.. Enter the following code into the script text field: bash ${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Mapbox.framework/strip-frameworks.sh
  • For iOS9 or later, make this change to your
    info.plist (Project target > info.plist > Add row and set key NSLocationWhenInUseUsageDescription, NSLocationAlwaysUsageDescription)

Add your MapmyIndia Map API keys to your AppDelegate.m as follows

  1. Add the following import statement.

Objective C

#import <MapmyIndiaAPIKit/MapmyIndiaAPIKit.h>

Swift

import MapmyIndiaAPIKit
  1. Add the following import statement: To initialize SDK you have to set required keys. You can achieve this using
    two ways:

First Way (Preferred)
By adding following keys in Info.plist file of your project MapmyIndiaSDKKey, MapmyIndiaRestKey, MapmyIndiaAtlasClientId, MapmyIndiaAtlasClientSecret, MapmyIndiaAtlasGrantType.

Second Way
You can also set these required keys programmatically. Add the following to your application:didFinishLaunchingWithOptions: method, replacing restAPIKey and mapSDKKey with your own API keys:

Objective C

[MapmyIndiaAccountManager setMapSDKKey:@"MAP SDK_KEY"];
[MapmyIndiaAccountManager setRestAPIKey:@"REST API_KEY"];
[MapmyIndiaAccountManager setAtlasClientId:@"ATLAS CLIENT_ID"];
[MapmyIndiaAccountManager setAtlasClientSecret:@"ATLAS CLIENT_SECRET"];
[MapmyIndiaAccountManager setAtlasGrantType:@"GRANT_TYPE"]; //always put client_credentials
[MapmyIndiaAccountManager setAtlasAPIVersion:@"1.3.11"]; // Optional; deprecated

Swift

MapmyIndiaAccountManager.setMapSDKKey("MAP SDK_KEY")
MapmyIndiaAccountManager.setRestAPIKey("REST API_KEY")
MapmyIndiaAccountManager.setAtlasClientId("ATLAS CLIENT_ID")
MapmyIndiaAccountManager.setAtlasClientSecret("ATLAS CLIENT_SECRET")
MapmyIndiaAccountManager.setAtlasGrantType("GRANT_TYPE") //always put client_credentials
MapmyIndiaAccountManager.setAtlasAPIVersion("1.3.11") // Optional; deprecated

Map View on view controller can be added either using interface builder or programmatically.

By Interface Builder

In a storyboard, add a view to your View Controller. In the Identity inspector, change its class to MapmyIndiaMapView.
Note :- Map style url need to be set at the time of load map

By Programmatically

To add map on view Controller create an instance of .MGLMapView. and add this to view of ViewController.

Objective C

#import
@interface ViewController () <MGLMapViewDelegate>
@end
@implementation ViewController

Swift

import Mapbox
  class ViewController: UIViewController, MGLMapViewDelegate {
  override func viewDidLoad() {
  super.viewDidLoad()
  let mapView = MapmyIndiaMapView(frame: view.bounds)
  mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  view.addSubview(mapView)
          }
     });

Note Also add app transport security exception in Info.plist of your project. Example: NSAppTransportSecurity NSAllowsArbitraryLoads

Set Zoom Level

Set zoom to 4 for country level display and 18 for house number display.

Objective C

mapView.zoomLevel = 4;

Swift

mapView.zoomLevel = 4

Current Location

To show user's current location on map, use property .showsUserLocation. and set its value to true.

Objective C

mapView.showsUserLocation = YES;

Swift

self.mapView.showsUserLocation = true

Tracking Mode

To move map with user location change use property userTrackingMode. Its an enum property of type .MGLUserTrackingMode.. You can set any value from following:

  • followWithCourse
  • followWithHeading
  • none

Code Snippet

mapView.userTrackingMode = .followWithCourse

The Map object should implement the methods of the MGLMapViewDelegate protocol corresponding to the events you wish it to receive. This delegate can also be used to detect map overlays selection. Delegate handles gesture events, tap on annotation (marker) and map center coordinates.

Map Position Changes Events

-(BOOL)mapView:(MGLMapView *)mapView shouldChangeFromCamera:(MGLMapCamera*)oldCamera toCamera:(MGLMapCamera *)newCamera;
-(BOOL)mapView:(MGLMapView *)mapView shouldChangeFromCamera:(MGLMapCamera*)oldCamera toCamera:(MGLMapCamera *)newCamera reason:(MGLCameraChangeReason)reason;
-(void)mapView:(MGLMapView *)mapView regionWillChangeAnimated:(BOOL)animated;
-(void)mapView:(MGLMapView *)mapView regionWillChangeWithReason:(MGLCameraChangeReason)reason animated:(BOOL)animated;
-(void)mapViewRegionIsChanging:(MGLMapView *)mapView;
-(void)mapView:(MGLMapView *)mapView regionIsChangingWithReason:(MGLCameraChangeReason)reason;
-(void)mapView:(MGLMapView *)mapView regionDidChangeAnimated:(BOOL)animated;`
-(void)mapView:(MGLMapView *)mapView regionDidChangeWithReason:(MGLCameraChangeReason)reason animated:(BOOL)animated;

Loading the Map Events

-(void)mapViewWillStartLoadingMap:(MGLMapView *)mapView;
-(void)mapViewDidFinishLoadingMap:(MGLMapView *)mapView;
-(void)mapViewDidFailLoadingMap:(MGLMapView *)mapView withError:(NSError*)error;
-(void)mapViewWillStartRenderingMap:(MGLMapView *)mapView;
-(void)mapViewWillStartRenderingFrame:(MGLMapView *)mapView;
-(void)mapViewDidFinishRenderingFrame:(MGLMapView *)mapView fullyRendered:(BOOL)fullyRendered;
-(void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle*)style;

Tracking User Location Events

-(void)mapViewWillStartLocatingUser:(MGLMapView *)mapView;
-(void)mapViewDidStopLocatingUser:(MGLMapView *)mapView;
-(void)mapView:(MGLMapView *)mapView didUpdateUserLocation:(nullableMGLUserLocation *)userLocation;
-(void)mapView:(MGLMapView *)mapView didFailToLocateUserWithError:(NSError*)error;
-(void)mapView:(MGLMapView *)mapView didChangeUserTrackingMode:(MGLUserTrackingMode)mode animated:(BOOL)animated;

Appearance of Annotations Events

-(nullable MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation;
-(CGFloat)mapView:(MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape*)annotation;
-(UIColor *)mapView:(MGLMapView *)mapView strokeColorForShapeAnnotation:(MGLShape *)annotation;
-(UIColor *)mapView:(MGLMapView *)mapView fillColorForPolygonAnnotation:(MGLPolygon *)annotation;
-(CGFloat)mapView:(MGLMapView *)mapView lineWidthForPolylineAnnotation:(MGLPolyline *)annotation;

Annotation Views Events

-(nullable MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id <MGLAnnotation>)annotation;
-(void)mapView:(MGLMapView *)mapView didAddAnnotationViews: (NS_ARRAY_OF(MGLAnnotationView *) *)annotationViews;

Selecting Annotations Events

-(void)mapView:(MGLMapView *)mapView didSelectAnnotation:(id <MGLAnnotation>)annotation;
-(void)mapView:(MGLMapView *)mapView didDeselectAnnotation:(id <MGLAnnotation>)annotation;
-(void)mapView:(MGLMapView *)mapView didSelectAnnotationView: (MGLAnnotationView *)annotationView;
-(void)mapView:(MGLMapView *)mapView didDeselectAnnotationView: (MGLAnnotationView *)annotationView;

Callout Views Events

-(BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation;
-(nullable id <MGLAnnotation>)mapView:(MGLMapView *)mapView calloutViewForAnnotation:(id <MGLAnnotation>)annotation;
-(nullable UIView *)mapView:(MGLMapView *)mapView leftCalloutAccessoryViewForAnnotation:(id  <MGLAnnotation>)annotation;
-(nullable UIView *)mapView:(MGLMapView *)mapView rightCalloutAccessoryViewForAnnotation:(id <MGLAnnotation>)annotation;
-(void)mapView:(MGLMapView *)mapView annotation:(id <MGLAnnotation>)annotation calloutAccessoryControlTapped:(UIControl *)control;
-(void)mapView:(MGLMapView *)mapView tapOnCalloutForAnnotation:(id <MGLAnnotation>)annotation;

Map Tap

To capture single tap on map events add a tap gesture to instance of MGLMapView.

Objective C

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapPress:)];
[self.mapView addGestureRecognizer:singleTap];

-(void)didTapPress:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateEnded == gesture.state) {
//Do Whatever You want on End of Gesture
   }
  }

Swift

let singleTap = UITapGestureRecognizer(target: self, action:
 #selector(didTapMap(tap:)))
 mapView.addGestureRecognizer(singleTap)
 @objc func didTapMap(tap: UITapGestureRecognizer) {
 if tap.state == .ended {
   //Do Whatever You want on End of Gesture
  }
}

Long Press on Map

To capture long press on map events add a long press gesture to instance of MGLMapView.

Objective C

UILongPressGestureRecognizer *longPress =  [[UILongPressGestureRecognizer
alloc] initWithTarget:self action:@selector(didLongPress:)];
[longPress setMinimumPressDuration:1.0];
[self.mapView addGestureRecognizer:longPress];
-(void)didLongPress:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateEnded == gesture.state) {
//Do Whatever You want on End of Gesture
   }
  }

Swift

let longPress = UILongPressGestureRecognizer(target: self, action:
#selector(didLongPress(tap:)))
mapView.addGestureRecognizer(longPress)
@objc func didLongPress(tap: UILongPressGestureRecognizer) {
if tap.state == .began {
//Do Whatever You want on End of Gesture
  }
}

Add marker

To show an annotation on map create an instance of MGLPointAnnotation and add that object to instance of MGLMapView using method .addAnnotation..

After creating instance of MGLPointAnnotation, set coordinate and title property values.

Objective C

MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
point.coordinate = CLLocationCoordinate2DMake(28.550834, 77.268918);
point.title = @"Annotation";
[self.mapView addAnnotation:point];

Swift

var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: 28.550834, longitude: 77.268918)
point.title = "Annotation"
mapView.addAnnotation(point)

Remove marker

Objective C

[self.mapView removeAnnotation:point];

Swift

mapView.removeAnnotation(point)

Custom Marker (Change Default Marker Icon)

To change image for default marker you can use delegate methods of protocol
MGLMapViewDelegate. Either you can override whole view of marker by using below method of delegate:

-(nullable MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id <MGLAnnotation>)annotation;

or you can override image of marker by using below method of delegate:

-(nullable MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation;

Show Info Window (Callout)

To enable info window on tap of marker return true from below delegate method:

- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation;

Custom Info Window (Changing Callout)

To change default callout view of annotation use delegate function of .MGLMapViewDelegate. protocol and return custom view of callout from that function.

func mapView(_ mapView: MGLMapView, calloutViewFor annotation:
MGLAnnotation) -> MGLCalloutView? {
return CustomCalloutView(representedObject: annotation)
}

To create custom callout view create a class inherited from .MGLCalloutView. and .UIView.. Override draw function of UIView to design your own callout view.

Add a Polyline

To show a polyline on map create an instance of MGLPolyline and add that object to instance of MGLMapView using method .addAnnotation..

To create instance of MGLPolyline an array of CLLocationCoordinate2D will be required so first create an array of CLLocationCoordinate2D.

Objective C

CLLocationCoordinate2D coordinates[] = {
CLLocationCoordinate2DMake(28.550834, 77.268918),
CLLocationCoordinate2DMake(28.551059, 77.268890),
CLLocationCoordinate2DMake(28.550938, 77.267641),
CLLocationCoordinate2DMake(28.551764, 77.267575),
CLLocationCoordinate2DMake(28.552068, 77.267599),
CLLocationCoordinate2DMake(28.553836, 77.267450),
};
NSUInteger numberOfCoordinates = sizeof(coordinates) / sizeof(CLLocationCoordinate2D);
MGLPolyline *polyline = [MGLPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
[self.mapView addAnnotation:polyline];

Swift

var coordinates = [
CLLocationCoordinate2D(latitude: 28.550834, longitude: 77.268918),
CLLocationCoordinate2D(latitude: 28.551059, longitude: 77.268890),
CLLocationCoordinate2D(latitude: 28.550938, longitude: 77.267641),
CLLocationCoordinate2D(latitude: 28.551764, longitude: 77.267575),
CLLocationCoordinate2D(latitude: 28.552068, longitude: 77.267599),
CLLocationCoordinate2D(latitude: 28.553836, longitude: 77.267450),
]
let polyline = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count))
mapView.addAnnotation(polyline)

Remove a Polyline

Objective C

[self.mapView removeAnnotation:polyline];

Swift

mapView.removeAnnotation(polyline)

Add a Polygon

To show a polygon on map create an instance of MGLPolygon and add that object to instance of MGLMapView using method .addAnnotation..

To create instance of MGLPolygon an array of CLLocationCoordinate2D will be required so first create an array of CLLocationCoordinate2D.

Objective C

CLLocationCoordinate2D coordinates[] = {
CLLocationCoordinate2DMake(28.550834, 77.268918),
CLLocationCoordinate2DMake(28.551059, 77.268890),
CLLocationCoordinate2DMake(28.550938, 77.267641),
CLLocationCoordinate2DMake(28.551764, 77.267575),
CLLocationCoordinate2DMake(28.552068, 77.267599),
CLLocationCoordinate2DMake(28.553836, 77.267450),
};
NSUInteger numberOfCoordinates = sizeof(coordinates) / sizeof(CLLocationCoordinate2D);
MGLPolygon *polygon = [MGLPolygon polygonWithCoordinates:coordinates count:numberOfCoordinates];//
[self.mapView addAnnotation:polygon];

Swift

var coordinates = [
CLLocationCoordinate2D(latitude: 28.550834, longitude: 77.268918),
CLLocationCoordinate2D(latitude: 28.551059, longitude: 77.268890),
CLLocationCoordinate2D(latitude: 28.550938, longitude: 77.267641),
CLLocationCoordinate2D(latitude: 28.551764, longitude: 77.267575),
CLLocationCoordinate2D(latitude: 28.552068, longitude: 77.267599),
CLLocationCoordinate2D(latitude: 28.553836, longitude: 77.267450),
]
let polygon = MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count))
mapView.addAnnotation(polygon)

Remove a Polygon

Objective C

[self.mapView removeAnnotation:polygon];

Swift

mapView.removeAnnotation(polygon)

For any queries and support, please contact:

Email Email us at apisupport@mapmyindia.com

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.

Clone this wiki locally