Getting started with MapKit for iOS

The MapKit library for iOS 9 and later is available at the CocoaPods repository. To create an application with a Yandex map:

  1. Step 1. Get the MapKit key
  2. Step 2. Add the library to your project
  3. Step 3. Set up the library
  4. Step 4. Build and run the application

Step 1. Get the MapKit key

  1. Log in to your Yandex account or register a new account.
  2. Click Get key, then choose MapKit SDK.
  3. Fill in the information about the project, select a pricing plan, and click Send.

You will receive further instructions by email.

The key you receive can be used to work with MapKit in several applications.

Step 2. Add the library to your project

Use CocoaPods to add the library to the project.

  1. Go to the directory with the Xcode project (the directory where the file with the xcodeproj extension is located).
  2. Create the Podfile to list dependencies from other libraries:
    $ pod init
  3. Open the Podfile in a text editor and add a dependency for your target:
      use_frameworks!
      # Main MapKit library.
      pod 'YandexMapKit', ' ~ > 3.2’ 
      # Additional MapKit modules.
      # Remove these dependencies if your app doesn't use them.
      pod 'YandexMapKitDirections', ' ~ > 3.2’ 
      pod 'YandexMapKitPlaces', ' ~ > 3.2’ 
      pod ‘YandexMapKitSearch', ' ~ > 3.2’ 
      pod 'YandexMapKitTransport', ' ~ > 3.2’ 

    The “YandexMapKit” library contains the main classes needed to work with MapKit. Other dependencies allow you to use additional MapKit features.

    Note.

    You can specify '~>3.1' instead of the version number. This means that the project will use the version 3.1 library or its newer minor versions, if any. If you specify only pod 'YandexMapKit' in the Podfile, the latest version of the library will be used.

  4. Run the following command in the project directory:
    $ pod install
    Note.

    Afterwards, you can use the command pod update to update the library version.

    After that, a file with the xcworkspace extension will appear in the project directory. Use this file to work with your project. Do not use the file with the xcodeproj extension.

    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:

  1. Add the View item to the storyboard.
  2. Specify the YMKMapView class for the added View.
  3. Request the necessary permissions in the info.plist file:
    NSLocationWhenInUseUsageDescription
    NSMotionUsageDescription
  4. Connect the MapKit library:
    import YandexMapKit
  5. Set your API key in the application:didFinishLaunchWithOptions method of the application delegate:
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {    YMKMapKit.setApiKey("Your API key")
  6. Initialize the map in the needed View controller:

    override func viewDidLoad() {
        super.viewDidLoad()
        
        mapView.mapWindow.map!.move(
            with: YMKCameraPosition.init(target: YMKPoint(latitude: 55.751574, longitude: 37.573856), zoom: 15, azimuth: 0, tilt: 0),
            animationType: YMKAnimation(type: YMKAnimationType.smooth, duration: 5),
            cameraCallback: nil)
    }

Step 4. Build and run the application

Build the application. You can run it on:

  • iOS devices.

    Go to Product → Destination, then select the device under the Device section.

  • An emulator.

    Go to Product → Destination, then select the simulator in the iOS Simulators section.

Run the application. To do this, select Product → Run.

Sample application

import UIKitimport Foundationimport YandexMapKit/** * This example shows a map and moves camera to the specified point. 
 * You need to specify the API key in the AppDelegate.swift file by using the YMKMapKit.setApiKey(MAPKIT_API_KEY) method.
 * Don't forget to request the required permissions. */class MapViewController: UIViewController {    @IBOutlet weak var mapView: YMKMapView!
    
    let TARGET_LOCATION = YMKPoint(latitude: 59.945933, longitude: 30.320045)        override func viewDidLoad() {        super.viewDidLoad()                mapView.mapWindow.map!.move(            with: YMKCameraPosition(target: TARGET_LOCATION, zoom: 15, azimuth: 0, tilt: 0),            animationType: YMKAnimation(type: YMKAnimationType.smooth, duration: 5),            cameraCallback: nil)    }}

The complete sample code is available in the GitHub repository.