Map SDK
Introduction
This repository contains a sample project of an app built for iOS, which is based on Mappls's Map and other differennt SDKs.
Welcome to the Mappls iOS SDK
Explore the largest directory of APIs & SDKs for maps, routes and search.
Mappls's APIs, SDKs, and live updating map data available for 238 nations give developers tools to build better mapping, navigation, and search experiences across platforms.
In this section you will find documentation for :-
- Detailed information about integration of Vector iOS Map.
- REST APIs module is a custom module for developers to consume our RESTful APIs within their applications, easily, and without having to develop such functionality from scratch.
- Detailed information related to how to integrate Mappls Widgets.
Map SDK
MapplsMap
is a SDK to display map, mark locations, enhance with custom data and even draw routes or other shapes on top.
Getting Started
Mappls's Map SDK for iOS lets you easily add Mappls Map and web services to your own iOS app. Mappls's Map 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 Mappls's Maps app for iOS. The SDK handles map tiles download and their display along with a bunch of controls and native gestures.
Setup your Project
Create a new project in Xcode.
Install libraries to your project which can be done in two ways either Manual or using Cocoapods.
Manual Procedure:
- Drag and drop the Mappls's Map SDK xcframework (MapplsMap.xcframework) to your project. It must be added in embedded binaries.
- Drag and drop the
MapplsAPIKit
xcframework to your project. It must be added in embedded binaries. It is a dependent xcframework.
Cocoapods:
- Add below lines in pod file of your project:
pod 'MapplsMap', '5.13.2'
- Run pod install or pod update (to update existing pods)
For iOS9 or later, make this change to your info.plist (Project target > info.plist > Add row and set keys
NSLocationAlwaysAndWhenInUseUsageDescription
,NSLocationWhenInUseUsageDescription
,NSLocationAlwaysUsageDescription
). As follows<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Your location is required</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is required</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is required</string>Message should be appropriate, according to use of your application.
Authorization
MapplsAPICore
It is required to set Mappls keys to use any MAPPL's SDK. Please refer to documenatation here.
Usage
Add a Mappls Map View
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 MapplsMapView
.
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 MapplsMapView
. and add this to view of ViewController
.
Objective C
#import
@interface ViewController () <MapplsMapViewDelegate>
@end
@implementation ViewController
Swift
import MapplsMap
class ViewController: UIViewController, MapplsMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MapplsMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(mapView)
}
});
Note Also to handle app transport security exception add NSAppTransportSecurity
in Info.plist of your project. As follows:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Map Interactions
Set Zoom Level
Set zoom to 4 for country level display and 18 for house number display.
Objective C
[mapView setZoom:4];
Swift
mapView.zoomLevel = 4
Set Map Center
To set Center of map reference code is below:
Objective C
[self.mapView setCenterCoordinate: CLLocationCoordinate2DMake(28.551438, 77.265119)];
Swift
mapView.centerCoordinate = CLLocationCoordinate2DMake(28.551438, 77.265119)
Map Features
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
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
Objective C
self.mapView.userTrackingMode = MGLUserTrackingModeFollowWithCourse;
Swift
mapView.userTrackingMode = .followWithCourse
Map Events
The Map object should implement the methods of the MapplsMapViewDelegate
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
[-mapViewWillStartLoadingMap:]
Tells the delegate that the map view will begin to load.
This method is called whenever the map view starts loading, including when a new style has been set and the map must reload.
Objective-C
- (void)mapViewWillStartLoadingMap:(nonnull MGLMapView *)mapView;
Swift
optional func mapViewWillStartLoadingMap(_ mapView: MGLMapView)
[-mapViewDidFinishLoadingMap:]
Tells the delegate that the map view has finished loading.
This method is called whenever the map view finishes loading, either after the initial load or after a style change has forced a reload.
Objective-C
- (void)mapViewDidFinishLoadingMap:(nonnull MGLMapView *)mapView;
Swift
optional func mapViewDidFinishLoadingMap(_ mapView: MGLMapView)
[-mapViewDidFailLoadingMap:withError:]
Tells the delegate that the map view was unable to load data needed for displaying the map.
This method may be called for a variety of reasons, including a network connection failure or a failure to fetch the style from the server. You can use the given error message to notify the user that map data is unavailable.
Objective-C
- (void)mapViewDidFailLoadingMap:(nonnull MGLMapView *)mapView
withError:(nonnull NSError *)error;
Swift
optional func mapViewDidFailLoadingMap(_ mapView: MGLMapView, withError error: Error)
[-mapViewWillStartRenderingFrame:]
Tells the delegate that the map view is about to redraw.
This method is called any time the map view needs to redraw due to a change in the viewpoint or style property transition. This method may be called very frequently. Therefore, your implementation of this method should be as lightweight as possible to avoid affecting performance.
Objective-C
- (void)mapViewWillStartRenderingFrame:(nonnull MGLMapView *)mapView;
Swift
optional func mapViewWillStartRenderingFrame(_ mapView: MGLMapView)
[-mapViewDidFinishRenderingFrame:fullyRendered:]
Tells the delegate that the map view has just redrawn.
This method is called any time the map view needs to redraw due to a change in the viewpoint or style property transition. This method may be called very frequently. Therefore, your implementation of this method should be as lightweight as possible to avoid affecting performance.
Objective-C
- (void)mapViewDidFinishRenderingFrame:(nonnull MGLMapView *)mapView
fullyRendered:(BOOL)fullyRendered;
Swift
optional func mapViewDidFinishRenderingFrame(_ mapView: MGLMapView, fullyRendered: Bool)
[-mapViewDidBecomeIdle:]
Tells the delegate that the map view is entering an idle state, and no more drawing will be necessary until new data is loaded or there is some interaction with the map.
Objective-C
- (void)mapViewDidBecomeIdle:(nonnull MGLMapView *)mapView;
Swift
optional func mapViewDidBecomeIdle(_ mapView: MGLMapView)
[-mapView:didFinishLoadingStyle:]
Tells the delegate that the map has just finished loading a style.
This method is called during the initialization of the map view and after any subsequent loading of a new style.
Objective-C
- (void)mapView:(nonnull MGLMapView *)mapView
didFinishLoadingStyle:(nonnull MGLStyle *)style;
Swift
optional func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle)
SDK Error Codes
Map will fail with following error codes
- MAPS_SDK_KEY_MISSING=1; //when map sdk key is not passed
- REST_API_KEY_MISSING=2; //when rest api key is not passed
- ATLAS_CLIENT_ID_MISSING=3; //when atlas client id is not passed
- ATLAS_CLIENT_SECRET_MISSING=4; //when atlas client secret is not passed
- ATLAS_GRANT_TYPE_MISSING=5; //* //when atlas grant type is not passed
- AUTHENTICATION_FAILED=7; //when map fails to authenticate itself with Mappls server
- UNKNOWN=8; // when map loading failed due to unknown error
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 Events
Single Tap Gesture
To capture single tap on map events add a tap gesture to instance of MapplsMapView
. Tapped CGPoint on map can be converted to CLLocationCoordinate object. Also if there is any mareker in tapped area that can also be retrieved. Code for reference to explain above features is below:
Objective C
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(didTapPress:)];
[self.mapView addGestureRecognizer:singleTap];
-(void)didTapPress:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateEnded == gesture.state) {
CGPoint touchPoint = [sender locationInView:self.mapView];
// Convert tapped area point to a CLLocationCoordinate object
CLLocationCoordinate2D location = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView;
// Also existing annotation or features can be checked on tapped area point
NSArray<id <MGLFeature>> * visibleFeatures = [self.mapView visibleFeaturesAtPoint: touchPoint];
if (visibleFeatures.count > 0) {
}
}
}
Swift
let singleTap = UITapGestureRecognizer(target: self, action:
#selector(didTapMap(tap:)))
mapView.addGestureRecognizer(singleTap)
@objc func didTapMap(tap: UITapGestureRecognizer) {
if tap.state == .ended {
let point = tap.location(in: mapView)
// Convert tapped area point to a CLLocationCoordinate object
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
// Also existing annotation or features can be checked on tapped area point
let visibleFeatures = mapView.visibleFeatures(at: point)
if visibleFeatures.count > 0 {
}
}
}
Long Tap Gesture
To capture long press on map events add a long press gesture to instance of MapplsMapView
.
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) {
CGPoint touchPoint = [sender locationInView:self.mapView];
// Convert tapped area point to a CLLocationCoordinate object
CLLocationCoordinate2D location = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView;
// Also existing annotation or features can be checked on tapped area point
NSArray<id <MGLFeature>> * visibleFeatures = [self.mapView visibleFeaturesAtPoint: touchPoint];
if (visibleFeatures.count > 0) {
}
}
}
Swift
let longPress = UILongPressGestureRecognizer(target: self, action:
#selector(didLongPress(tap:)))
mapView.addGestureRecognizer(longPress)
@objc func didLongPress(tap: UILongPressGestureRecognizer) {
if tap.state == .ended {
let point = tap.location(in: mapView)
// Convert tapped area point to a CLLocationCoordinate object
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
// Also existing annotation or features can be checked on tapped area point
let visibleFeatures = mapView.visibleFeatures(at: point)
if visibleFeatures.count > 0 {
}
}
}
Map Overlays
Add marker
To show an annotation on map create an instance of MGLPointAnnotation
and add that object to instance of MapplsMapView
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
MapplsMapViewDelegate
.
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(#Custom-Info-Window)
To change default callout view of annotation use delegate function of MapplsMapViewDelegate.
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.
Polylines
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)
Polyline Width
Line width in points can be set when rendering the outline of a polyline annotation. It cab be achieve by using delegate method lineWidthForPolylineAnnotation
.
By default, the polyline is outlined with a line 3.0 points wide.
Objective C
- (CGFloat)mapView:(MGLMapView *)mapView lineWidthForPolylineAnnotation:(MGLPolyline *)annotation
{
return 10.0;
}
Swift
func mapView(_ mapView: MGLMapView, lineWidthForPolylineAnnotation annotation: MGLPolyline) -> CGFloat {
return 10.0
}
Polygons
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)
Map Camera
Fit Map Camera
showAnnotations
function sets the visible region so that the map displays the specified annotations.
Swift
mapView.showAnnotations(annotations, animated: true)
Create Camera For Shape
A camera object that best fits the given shape with some additional padding on each side while looking in the specified direction can be retrieved using function cameraThatFitsShape
of MapplsMapView
.
After creating a camera object using above function it can be set to fit map visible area.
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];
MGLMapCamera *shapeCam = [self.mapView cameraThatFitsShape:polygon direction:(0) edgePadding:UIEdgeInsetsMake(20, 20, 20, 20)];
[self.mapView setCamera:shapeCam];
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))
let shapeCam = mapView.cameraThatFitsShape(polyline, direction: CLLocationDirection(0), edgePadding: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
mapView.setCamera(shapeCam, animated: false)
Miscellaneous
Shape Stroke Color
Color of polyline
and stroke color of polygon
can be changed by using delegate method strokeColorForShapeAnnotation
.
The default stroke color is the map view’s tint color.
Objective C
- (UIColor *)mapView:(MGLMapView *)mapView strokeColorForShapeAnnotation:(MGLShape *)annotation {
return [UIColor redColor];
}
Swift
func mapView(_ mapView: MGLMapView, strokeColorForShapeAnnotation annotation: MGLShape) -> UIColor {
return .red
}
Shape Fill Color
Fill Color of polygon
can be changed by using delegate method fillColorForPolygonAnnotation
.
The default fill color is the map view’s tint color.
Objective C
- (UIColor *)mapView:(MGLMapView *)mapView fillColorForPolygonAnnotation:(MGLPolygon *)annotation {
return [UIColor redColor];
}
Swift
func mapView(_ mapView: MGLMapView, fillColorForPolygonAnnotation annotation: MGLPolygon) -> UIColor {
return UIColor.red
}
Shape Opacity
To change opacity of a shape i.e polyline or polygon, delegate method alphaForShapeAnnotation
can be used.
It sets the opacity of an entire shape, inclusive of its stroke and fill.
Objective C
- (CGFloat)mapView:(MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape *)annotation
{
return 0.5;
}
Swift
func mapView(_ mapView: MGLMapView, alphaForShapeAnnotation annotation: MGLShape) -> CGFloat {
return 0.5
}
Safety Status Strip
A safety status strip can be show on map based on user's location. COVID-19 status will be shown on map currently, It will show whether user's location lies in a containment zone or not and conaintment zone info like Containment Zone Name, District etc. For more info about how to use go to Safety Strip.
Cluster Based Authentication
Map SDK can be configured such that it can work only for specified unique identifier of a device and will show an error accordingly.
Set Cluster Device
Cluster Identifier can be set and retrieve using singelton class MapplsAccountManager
which is part of Map SDK's dependency MapplsAPICore
.
Below is code for reference to set and get cluster identifier.
Objective C
// Set Cluster Identifier
[MapplsAccountManager setClusterId:@"YOUR_CLUSTER_DEVICE_IDENTIFIER"];
// Get Cluster Identifier
NSString * clusterId = [MapplsAccountManager clusterId];
Swift
// Set Cluster Identifier
MapplsAccountManager.setClusterId("YOUR_CLUSTER_DEVICE_IDENTIFIER")
// Get Cluster Identifier
let clusterId = MapplsAccountManager.clusterId()
SwiftUI
Create Map View
In your project, add new SwiftUI View and name it MapView.swift
In order to use native UIKit views in
SwiftUI
view, you must use UIViewRepresentable wrapper. The instance of custom type which adopts UIViewRepresentable protocol is responsible for creation and management a UIView object in your SwiftUI interface.struct MapView: UIViewRepresentable {
...
}The UIViewRepresentable requires to implement makeUIViewController(context:) method that creates the instance of with the desired UIKit view. Add the following code to create map view instance
private let mapView: MapplsMapView = MapplsMapView(frame: .zero)
func makeUIView(context: UIViewRepresentableContext<MapView>) -> MapplsMapView {
mapView.delegate = context.coordinator
return mapView
}The UIViewRepresentable view also requires to implement updateUIView(_:context:) which is used to configure the newly created instance. To show map only We dont need to configure anything so we will keep it empty.
func updateUIView(_ uiView: MapplsMapView, context: UIViewRepresentableContext<MapView>) {}
To add map annoations at design time add an annotations
property in MapView
struct as below:
@Binding var annotations: [MGLPointAnnotation]
Use below code to add marker at design time by setting annotations property.
struct ContentView: View {
@State var annotations: [MGLPointAnnotation] = [
MGLPointAnnotation(title: "MAPPLS", coordinate: .init(latitude: 28.550679, longitude: 77.268949))
]
var body: some View {
MapView(annotations: $annotations).centerCoordinate(.init(latitude: 28.550679, longitude: 77.268949)).zoomLevel(16)
}
}
Respond To Map Events
In order to to respond to map events, for example perform an action after MapView initialization finished. In SwiftUI, a Coordinator can be used with delegates, data sources, and user events. The UIViewRepresentable protocol defines makeCoordinator() method which creates coordinator instance. Add the following code to declare coordinator class:
class Coordinator: NSObject, MGLMapViewDelegate {
var control: MapView
init(_ control: MapView) {
self.control = control
}
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
// write your custom code which will be executed
// after map's style has been loaded
}
}
And then add the following method to the SwiftUI view:
func makeCoordinator() -> MapView.Coordinator {
Coordinator(self)
}
And finally set the reference coordinator on Mappls map view.
mapView.delegate = context.coordinator