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 HMS Core 3.0 Integration Preparation.

  1. Open Android Studio, go to File > New > New Project and select Empty Activity to create a project.
  2. Configure the Maven repository address for the HMS SDK.
  3. Setup 1 Open the build.gradle file in the root directory of your Android Studio project.

    Setup 2 Go to allprojects > repositories and configure the Maven repository address for the HMS SDK.

    allprojects { repositories { google() jcenter() maven {url 'http://developer.huawei.com/repo/'} } }

    Setup 3 Go to buildscript > dependencies and add configurations.

    buildscript { dependencies {classpath 'com.huawei.agconnect:agcp:1.0.0.300'} }
  4. Add compile dependencies.
    Setup 1 Open the build.gradle file in the app directory of your project.
  5. Setup 2 Configure compile dependencies in dependencies.

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

    Setup 3 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.
    <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.
  3. 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>
  4. Add the configuration for calling the MapView to the activity.
  5. 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); } 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; } }
  6. After map initialization you can move the camera to the location you want to show
    // move camera by CameraPosition param ,latlag and zoom params can set here CameraPosition build = new CameraPosition.Builder().target(new LatLng(60, 60)).zoom(5).build(); CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(build); hmap.animateCamera(cameraUpdate); hmap.setMaxZoomPreference(5); hmap.setMinZoomPreference(2);
  7. After map initialization you can add marker to the map
    // mark can be add by HuaweiMap mMarker = hmap.addMarker(new MarkerOptions().position(LAT_LNG) .alpha(0.5f) .icon(BitmapDescriptorFactory.fromResource(R.drawable.badge_ph)) .infoWindowAnchor(0.5f, 0.5f) .clusterable(true)); mMarker.setAnchor(0, -1); mMarker.showInfoWindow();
  8. After map initialization you can draw a circle on the map
    // circle can be add by HuaweiMap mCircle = hmap .addCircle(new CircleOptions().center(new LatLng(60, 60)).radius(500).fillColor(0XFF00FFFF).strokeWidth(10).strokeColor( Color.RED));

Compiling, Loading, and Debugging the App

Double-click assemble in the Gradle compilation tool provided by Android Studio.

Find the generated app, open the command window, and run the adb install app-debug.apk command.

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

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

To download the sample code, click the following link:

Download

Code copied