-
Notifications
You must be signed in to change notification settings - Fork 6
Home
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.
- 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 keyNSLocationWhenInUseUsageDescription
,NSLocationAlwaysUsageDescription
)
Add your MapmyIndia Map API keys to your AppDelegate.m
as follows
- Add the following import statement.
#import <MapmyIndiaAPIKit/MapmyIndiaAPIKit.h>
import MapmyIndiaAPIKit
- 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:
[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
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.
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
To add map on view Controller create an instance of .MGLMapView
. and add this to view of ViewController
.
#import
@interface ViewController () <MGLMapViewDelegate>
@end
@implementation ViewController
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 to 4 for country level display and 18 for house number display.
mapView.zoomLevel = 4;
mapView.zoomLevel = 4
To show user's current location on map, use property .showsUserLocation.
and set its value to true.
mapView.showsUserLocation = YES;
self.mapView.showsUserLocation = true
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
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.
-(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;
-(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;
-(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;
-(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;
-(nullable MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id <MGLAnnotation>)annotation;
-(void)mapView:(MGLMapView *)mapView didAddAnnotationViews: (NS_ARRAY_OF(MGLAnnotationView *) *)annotationViews;
-(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;
-(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;
To capture single tap on map events add a tap gesture to instance of MGLMapView
.
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
}
}
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
}
}
To capture long press on map events add a long press gesture to instance of MGLMapView
.
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
}
}
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
}
}
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.
MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
point.coordinate = CLLocationCoordinate2DMake(28.550834, 77.268918);
point.title = @"Annotation";
[self.mapView addAnnotation:point];
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: 28.550834, longitude: 77.268918)
point.title = "Annotation"
mapView.addAnnotation(point)
[self.mapView removeAnnotation:point];
mapView.removeAnnotation(point)
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;
To enable info window on tap of marker return true from below delegate method:
- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation;
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.
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
.
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];
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)
[self.mapView removeAnnotation:polyline];
mapView.removeAnnotation(polyline)
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
.
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];
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)
[self.mapView removeAnnotation:polygon];
mapView.removeAnnotation(polygon)
For any queries and support, please contact:
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.