HMS Core Map SDK is a set of APIs for map development in Android. The map data covers most countries outside China and supports multiple languages. The Map SDK uses the WGS 84 GPS coordinate system, which can meet most requirements of map development outside China. 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 HUAWEI 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, click My projects, click your project, and go to Project Setting > Manage APIs.
  2. Toggle on the Map Kit switch.

You have now successfully enabled HUAWEI Map Kit for your app.

1. Add the AppGallery Connect configuration file of your app.

(a) Download the agconnect-services.json file of your app.

(b) Add agconnect-services.json to the app directory in your project.

2. Add build dependencies.

(a) Open the build.gradle file in the app directory.

(b) Add build dependencies in the dependencies section.

dependencies { implementation 'com.huawei.hms:maps:{version}' }

(c) Add apply plugin: 'com.huawei.agconnect' to the file header.

  1. Apply for relevant permissions in sections at the same level as the application section 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 path: \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:android="http://schemas.android.com/apk/res/android" 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 the MapView to the activity. File path: \mapdemo\app\src\main\java\com\wz\android\mapdemo\MainActivity.java
    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; import com.huawei.hms.maps.util.LogM; /** * 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) { LogM.d(TAG, "onCreate:hzj"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //get 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 api_key field value in // agconnect-services.json MapsInitializer.setApiKey("Your API key"); mMapView.onCreate(mapViewBundle); //get map instance mMapView.getMapAsync(this); } @Override public void onMapReady(HuaweiMap map) { //get map instance in a callback method Log.d(TAG, "onMapReady: "); hMap = map; } }
  4. Add the life cycle method of the MapView.
    @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(); }
  5. Verify that required permissions have been assigned.
    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; }

Building, Loading, and Debugging the App

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

Preparing for the Debugging

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

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

1. Why is result code 010001 or 010003 returned?

The possible causes are as follows:

2. For more information about HUAWEI Map Kit, click the following link:


3. You can click the button below to download the source code.

Source code

Code copied