Overview

The Map SDK provides a set of APIs for map development in Android. The map data covers most countries and regions outside the Chinese mainland, and supports multiple languages. Map Kit uses the WGS 84 GPS coordinate system, which can meet most map development requirements outside the Chinese mainland. You can easily add map-related functions in your Android app, including:

What You Will Create

In this codelab, you can develop map-related apps, for example:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To integrate Map Kit, you must complete the following preparations:

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

1. - Sign in to AppGallery Connect and click My projects.
2. - Find your project from the project list and click the app for which you need to enable Map Kit on the project card.
3. - Go to Project settings > Manage APIs and enable Map Kit.

Adding the AppGallery Connect Configuration File of Your App

  1. Download the agconnect-services.json file of your app.
  2. Add the agconnect-services.json file to the app's root directory of your project.

Adding Build Dependencies

  1. Open the app-level build.gradle file of your project.
  2. Add build dependencies in the dependencies block.
    dependencies { implementation 'com.huawei.hms:maps:{version}' }
  3. Add apply plugin: ‘com.huawei.agconnect' to the file header.
  1. Declare relevant permissions in blocks at the same level as the application block in the AndroidManifest.xml file.
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  2. Add MapView to the layout file. The file path is \mapdemo\app\src\main\res\layout\activity_main.xml.
    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.huawei.hms.maps.MapView xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" map:cameraTargetLat="51" map:cameraTargetLng="10" map:cameraZoom="8.5" map:mapType="normal" map:uiCompass="true" map:uiZoomControls="true" /> </LinearLayout>
  3. Add the configuration for calling MapView to the activity file. The file path is \mapdemo\app\src\main\java\com\wz\android\mapdemo\MainActivity.java.
    Java sample code:
    package com.wz.android.mapdemo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import com.huawei.hms.maps.HuaweiMap; import com.huawei.hms.maps.MapView; import com.huawei.hms.maps.OnMapReadyCallback; /** * Map activity entrance class. */ public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { private static final String TAG = "MapViewDemoActivity"; // Huawei map. private HuaweiMap hMap; private MapView mMapView; private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Obtain a MapView instance. mMapView = findViewById(R.id.mapView); Bundle mapViewBundle = null; if (savedInstanceState != null) { mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY); } // Please replace Your API key with the API key in // agconnect-services.json. MapsInitializer.setApiKey("Your API key"); mMapView.onCreate(mapViewBundle); // Obtain a map instance. mMapView.getMapAsync(this); } @Override public void onMapReady(HuaweiMap map) { // Obtain a map instance from callback. Log.d(TAG, "onMapReady: "); hMap = map; } }

    Kotlin sample code:

    package com.huawei.mapkitsample import android.os.Bundle import android.util.Log import androidx.appcompat.app.AppCompatActivity import com.huawei.hms.maps.CameraUpdateFactory import com.huawei.hms.maps.HuaweiMap import com.huawei.hms.maps.MapView import com.huawei.hms.maps.OnMapReadyCallback import com.huawei.hms.maps.model.LatLng import com.huawei.hms.maps.util.LogM /** * Map activity entrance class. */ class MapViewDemoActivity : AppCompatActivity(), OnMapReadyCallback { // HUAWEI map private var hMap: HuaweiMap? = null private var mMapView: MapView? = null companion object { private const val TAG = "MapViewDemoActivity" private const val MAPVIEW_BUNDLE_KEY = "MapViewBundleKey" } override fun onCreate(savedInstanceState: Bundle?) { LogM.d(TAG, "onCreate:") super.onCreate(savedInstanceState) setContentView(R.layout.activity_mapview_demo) mMapView = findViewById(R.id.mapView) var mapViewBundle: Bundle? = null if (savedInstanceState != null) { mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY) } mMapView?.apply { onCreate(mapViewBundle) getMapAsync(this@MapViewDemoActivity) } } override fun onMapReady(map: HuaweiMap) { Log.d(TAG, "onMapReady: ") hMap = map } }
  4. Add the lifecycle methods of MapView. The file path is \mapdemo\app\src\main\java\com\wz\android\mapdemo\MainActivity.java.
    Java sample code:
    @Override protected void onStart() { super.onStart(); mMapView.onStart(); } @Override protected void onStop() { super.onStop(); mMapView.onStop(); } @Override protected void onDestroy() { super.onDestroy(); mMapView.onDestroy(); } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onResume() { super.onResume(); mMapView.onResume(); }

    Kotlin sample code:

    override fun onStart() { super.onStart() mMapView?.onStart() } override fun onStop() { super.onStop() mMapView?.onStop() } override fun onDestroy() { super.onDestroy() mMapView?.onDestroy() } override fun onPause() { mMapView?.onPause() super.onPause() } override fun onResume() { super.onResume() mMapView?.onResume() }
  5. Verify that required permissions have been assigned. The file path is \mapdemo\app\src\main\java\com\wz\android\mapdemo\MainActivity.java.
    Java sample code:
    private static boolean hasPermissions(Context context, String... permissions) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permissions != null) { for (String permission : permissions) { if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { return false; } } } return true; }

    Kotlin sample code:

    private fun hasPermissions(context: Context, vararg permissions: String): Boolean { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { for (permission in permissions) { if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { return false } } } return true }

Building, Loading, and Debugging the App

Open Gradle built in Android Studio and double-click installDebug to install the developed demo app.

Open the developed app and check whether it can display a map properly.

Well done. You have successfully completed this codelab and learned how to:

  1. Why is result code 010001 or 010002 returned?
  2. For details about other result codes, please refer to Result Codes.
  3. For more information about Map Kit, please refer to Map Kit Development Guide.
  4. You can click here to download the source code.
Code copied