Getting started with MapKit for Android

The MapKit library for Android 4.0.3 and later is available at the Maven Central repository. To create an application with a Yandex map:

  1. Step 1. Get the MapKit key
  2. Step 2. Install the MapKit library
  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. Install the MapKit library

  1. Create a new project or open an existing one, for example, in Android Studio.
  2. Open the project's build.gradle file. In the repositories section, add a Maven Central repository:
    repositories {
        ...
        mavenCentral()
    }
  3. Open the applications (modules) build.gradle file. Specify the repository address and add a dependency:
    repositories {    maven {        url "http://maven.google.com/"    }}dependencies {    ...
        // Main MapKit library.
        implementation ' com.yandex.android: mapkit: 3.3.1'
        // Additional MapKit modules.
        // Remove these dependencies if your app doesn't use them.
        implementation 'com.yandex.android:directions:3.3.1'
        implementation 'com.yandex.android:places:3.3.1'
        implementation 'com.yandex.android:search:3.3.1'
        implementation 'com.yandex.android:transport:3.3.1'
        // Exclicitly implement the play-services-location library
        implementation 'com.google.android.gms:play-services-location:16.0.0'
    }
  4. Synchronize the project to apply the changes. For example, in Android Studio, you can click Sync Now or select File → Synchronize from the menu. Wait for synchronization to finish.

If synchronization succeeds, the library is automatically added to the project when it is compiled. If compilation fails, make sure that you specified the repository and dependency correctly and synchronize the project again.

Step 3. Set up the library

  1. Add the map to your Activity:
    <com.yandex.mapkit.mapview.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
  2. Initialize the map in the onCreatemethod of the needed Activity:
    private MapView mapview;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    MapKitFactory.setApiKey ("your API key");    MapKitFactory.initialize(this);    // Specify the name of the activity in place of map.    setContentView (R. layout.map);    mapview = (MapView)findViewById (R. id.mapview);    mapview.getMap ().move(        new CameraPosition(new Point (55.751574, 37.573856), 11.0 f, 0.0 f, 0.0 f),        new Animation (Animation.Type.SMOOTH, 0),        null);}
    Note. We don't recommend calling MapKitFactory.initialize() in the Application.onCreate() method. This can lead to unnecessary operations and increased battery use.
  3. Send onStart and onStop events to MapKitFactory and mapView. Otherwise, MapKit will not be able to display the map and stop processing the map when the Activity with the map becomes invisible to the user:
    @Override
    protected void onStop() {
        super.onStop();
        mapview.onStop();
        MapKitFactory.getInstance().onStop();
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        mapview.onStart();
        MapKitFactory.getInstance().onStart();
    }

Step 4. Build and run the application

When working in Android Studio, you can start the application immediately. The build will be performed automatically. Follow the instructions below to run the app on:

Sample application

package com.yandex.mapkitdemo;import android.os.Bundle;import android.app.Activity;import com.yandex.mapkit.Animation;import com.yandex.mapkit.MapKitFactory;import com.yandex.mapkit.geometry.Point;import com.yandex.mapkit.map.CameraPosition;import com.yandex.mapkit.mapview.MapView;/** * This example shows a map and moves the camera to the specified point. 
 * Don't forget to request the necessary permissions. */public class MapActivity extends Activity {    /**     * Replace "your_api_key" with a valid API key.
     * You can get the key on the website https://developer.tech.yandex.ru/     */    private final String MAPKIT_API_KEY = " your_api_key";    private final Point TARGET_LOCATION = new Point(59.945933, 30.320045);    private MapView mapView;    @Override    protected void onCreate(Bundle savedInstanceState) {        /**        * Set the API key before you initialize the MapKitFactory.
        * We recommend setting the key in the Application.onCreate method,        * but in this example, it is set in the activity.
        */        MapKitFactory.setApiKey(MAPKIT_API_KEY);        /**        * Initializes libraries to load the required native libraries.
        * It is recommended to initialize MapKit in the Activity.onCreate method         * Initializing in the Application.onCreate method can lead to extra calls and increased battery usage.
        */        MapKitFactory.initialize(this);        // Creating the MapView.        setContentView(R. layout.map);        super.onCreate(savedInstanceState);        mapView = (MapView)findViewById (R id.mapview);        // Moving the camera to the center of Saint Petersburg.        mapView.getMap ().move(                new camera Position(TARGET_LOCATION, 14.0 f, 0.0 f, 0.0 f),                new Animation (Animation.Type.SMOOTH, 5),                null);    }    @Override    protected void onStop() {        // You need to pass calls of the onStop method to MapView and MapKit instances.        mapView.onStop();        MapKitFactory.getInstance ().onStop();        super.onStop();    }    @Override    protected void onStart() {        // You need to pass calls of the onStart method to MapView and MapKit instances.        super.onStart();        MapKitFactory.getInstance ().onStart();        mapView.onStart();    }}     

The complete sample code is available in the GitHub repository.