Overview

The HMS Map Kit is a set of interfaces for map development in Android. The map data covers most countries outside China and supports multiple languages. The HMS Map Kit uses the WGS84 GPS coordinate system, which can meet most map development requirements 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

Hardware Requirements

Software Requirements

Creating an App and Obtaining the Configuration File

Create an app in AppGallery Connect and obtain the project configuration file agconnect-services.json. The procedure is as follows:

For details, see the Preparations for Integrating HUAWEI HMS Core.

1.Open Android Studio, go to File > New > New Project and select Empty Activity to create a project.

Select API 19 as Mimimum API level.

2.Configure the Maven repository address for the HMS SDK.

3.Add compile dependencies.
(a) Open the build.gradle file in the app directory of your project.

(b) Configure compile dependencies in dependencies.

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

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

1.Add permission application configurations in tags at the same level as that of the application tag in the AndroidManifest.xml file.

And change allowBackup property in the AndroidManifest.xml file to false, eg, android:allowBackup="false".

<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 location: \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:mapType="normal" map:uiCompass="true" map:uiZoomControls="true" map:cameraTargetLat="51" map:cameraTargetLng="10" map:cameraZoom="8.5"/> </LinearLayout>

3.Add the configuration for calling the MapView to the activity.

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[] RUNTIME_PERMISSIONS = { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET }; 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); if (!hasPermissions(this, RUNTIME_PERMISSIONS)) { ActivityCompat.requestPermissions(this, RUNTIME_PERMISSIONS, REQUEST_CODE); } //get mapview instance mMapView = findViewById(R.id.mapView); Bundle mapViewBundle = null; if (savedInstanceState != null) { mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_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.You need to determine whether the required permissions have been applied.

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; }

Compiling, Loading, and Debugging the App

Click Run in the tool bar provided by Android Studio.

Preparing for Debugging

Open the demo map app and check whether the map can be properly displayed.

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

You can read the links as follow for more information.

documentation

FAQs

1.The authentication server has a cache mechanism. If authentication fails before successful registration, incorrect data will be stored on the server for 24 hours and the registration will always fail in this period.
However, you can directly visit the following link and check whether the data is correct. You need to replace the value of appId in the link.
https://api.cloud.huawei.com/rest.php?nsp_svc=nsp.scope.app.get&nsp_fmt=json&appid=101023239&type=2

You can download the source code in github.com/Huawei/Consumer/tree/master/Codelabs/MapKit

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

Download

Code copied