Getting started with MapKit for iOS
The MapKit library for iOS 12 and later is available in the CocoaPods repository. To create an application with a Mappable map:
Step 1. Get the MapKit API key
Before you can use MapKit SDK in your application, you need the API key.
Go to the Mappable Account and register a new account or log in to an existing. The main page will display a key that is suitable for any Mappable service.
Step 2. Add the library to your project
Use CocoaPods to add the library to the project.
-
Go to the directory with the Xcode project.
-
Create the Podfile to list dependencies from other libraries:
pod init
-
Open the Podfile in a text editor and add a dependency for your target:
use_frameworks! # The lite library only contains the map, traffic layer, # LocationManager, and UserLocationLayer and lets you download offline maps (in the paid version only). pod 'MappableMobile', '1.0.2-lite' # The full library supplements lite version features with car routing, # bike routing, pedestrian routing, and public transport routing, # search, suggest, geocoding, and panorama display. # pod 'MappableMobile', '1.0.2-full'
-
Run the following command in the project directory:
pod install
To open the project file, run the following command:
open *.xcworkspace
Step 3. Set up the library
To use the library in your application, initialize it with the specified parameters. Open xcworkspace
and make the following changes:
-
Add the View item to the storyboard.
-
Specify the
MMKMapView
class for the added View. -
Request permission to determine the location and motion activity in the
Info.plist
file:<key>NSLocationWhenInUseUsageDescription</key> <string>This app displays user location arrow</string> <key>NSMotionUsageDescription</key> <string>It helps identify the mode of transport and the end of the trip more accurately</string>
-
Connect the MapKit library:
SwiftObjective-Cimport MappableMobile
#import <MappableMobile/MMKMapKitFactory.h>
-
Set your API key in the
application:didFinishLaunchingWithOptions
method of the application delegate and instantiate theMMKMapKit
object:SwiftObjective-Cfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { MMKMapKit.setApiKey("Your API key") MMKMapKit.sharedInstance() }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [MMKMapKit setApiKey: @"Your API key"]; [MMKMapKit mapKit]; }
Note
If you're using a multi-module application and want to initialize MapKit in a method different from
application:didFinishLaunchingWithOptions
, call theonStart()
method after creating the MapKit object:SwiftMMKMapKit.setApiKey("MAPKIT_API_KEY") MMKMapKit.sharedInstance().onStart()
-
Initialize the map in the needed View controller:
SwiftObjective-Coverride func viewDidLoad() { super.viewDidLoad() mapView.mapWindow.map!.move( with: MMKCameraPosition.init(target: MMKPoint(latitude: 55.751574, longitude: 37.573856), zoom: 15, azimuth: 0, tilt: 0), animation: MMKAnimation(type: MMKAnimationType.smooth, duration: 5), cameraCallback: nil) }
- (void)viewDidLoad { [super viewDidLoad]; MMKPoint *target = [MMKPoint pointWithLatitude:55.751574 longitude:37.573856]; [self.mapview.mapWindow.map moveWithCameraPosition:[MMKCameraPosition cameraPositionWithTarget:target zoom:11 azimuth:0 tilt:0]]; }
Step 4. Build and run the application
Build the application. You can run it on:
-
iOS devices.
Go to Product → Destination and select the device in the Device section.
-
Emulators.
Go to Product → Destination and select the simulator in the iOS Simulators section.
Run the application. To do this, select Product → Run.
Step 5. Note the following
MapKit stores weak references to the Listener objects passed to it. You need to store references to them in memory yourself:
Note
All Listener objects must be inherited from the NSObject
class.
internal class CameraListner: NSObject, MMKMapCameraListener {
func onCameraPositionChanged(with map: MMKMap?, cameraPosition: MMKCameraPosition, cameraUpdateReason: MMKCameraUpdateReason, finished: Bool) {
// Do something
}
}
let cameraListener = CameraListner()
override func viewDidLoad() {
super.viewDidLoad()
mapView.mapWindow.map!.addCameraListener(with: cameraListener)
}
@interface CameraListener: NSObject<MMKMapCameraListener>
@end
@implementation CameraListener
- (void)onCameraPositionChangedWithMap:(nullable MMKMap *)map cameraPosition:(nonnull MMKCameraPosition *)cameraPosition cameraUpdateReason:(MMKCameraUpdateReason)cameraUpdateReason finished:(BOOL)finished {
// Do something
}
@end
@property(nonatomic) CameraListener *cameraListener;
- (void)viewDidLoad {
[super viewDidLoad];
self.cameraListener = [[CameraListener alloc] init];
[self.mapView.mapWindow.map addCameraListenerWithCameraListener:self.cameraListener];
}
Note
-
By default, the methods of any Listener objects and platform interfaces are called on the main thread unless the method documentation specifies otherwise.
-
The minimum supported version for the M1 emulator is iOS 13.
-
The M1 emulator doesn't support OpenGL, so make sure to pass
vulkanPreferred: true
to theMMKMapView
andMMKPanoView
constructors. This setting is only required for the emulator build. -
Recommended linking flag:
-ObjC
.
Sample application
import UIKit
import Foundation
import MappableMobile
/**
* This example shows a map and moves the camera to the specified point.
* You need to specify the API key in the AppDelegate.swift file by using the MMKMapKit.setApiKey(MAPKIT_API_KEY) method.
* Be sure to request the required permissions.
*/
class MapViewController: UIViewController {
@IBOutlet weak var mapView: MMKMapView!
let TARGET_LOCATION = MMKPoint(latitude: 59.945933, longitude: 30.320045)
override func viewDidLoad() {
super.viewDidLoad()
mapView.mapWindow.map!.move(
with: MMKCameraPosition(target: TARGET_LOCATION, zoom: 15, azimuth: 0, tilt: 0),
animation: MMKAnimation(type: MMKAnimationType.smooth, duration: 5),
cameraCallback: nil)
}
}
The complete sample code is available in the GitHub repository.