Overview

The Map Java SDK for HarmonyOS provides Java APIs for HarmonyOS 2.0 or later. 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 HarmonyOS 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

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

For details, please refer to the development preparations in the Map Kit development guide.
  1. Sign in to AppGallery Connect and click My projects.
  2. Find your project and click the app for which you want to enable Map Kit.
  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-harmony:{version}' }
  1. Add permissions.
    a) Declare the following permission in the module block in the config.json file:
    <!–Allow the app to use the map service bridging capability–>
    "allowClassMap": true
    b) Declare the following permissions in the reqPermissions block in the config.json file:
    <!--Allow the app to use network sockets.--> ohos.permission.INTERNET <!--Allow the app to query the network status.--> ohos.permission.GET_NETWORK_INFO

    The sample code is as follows:

    { "module": { "allowClassMap": true, "reqPermissions": [ { "name": "ohos.permission.INTERNET", "reason": "access internet", "usedScene": { "ability": [ "com.mycamera.MainAbility" ], "when": "always" } }, { "name": "ohos.permission.GET_NETWORK_INFO", "reason": "GET_NETWORK_INFO", "usedScene": { "ability": [ "com.mycamera.MainAbility" ], "when": "always" } } ] } }
  2. In MainAbilitySlice, load MapView in the onStart(Intent intent) method and call getMapAsync() to register a callback. Call the onMapReady callback to obtain a HuaweiMap object. Before creating a MapView object, call CommonContext.setContext(this); to save the ability, so that map functions can be initialized.
    public void onStart(Intent intent) { super.onStart(intent); CommonContext.setContext(this); MapView mapView = new MapView(this); // Create a MapView object. mapView.onCreate(); // Obtain a HuaweiMap object. mapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(HuaweiMap huaweiMap) { HuaweiMap mHuaweiMap = huaweiMap; mHuaweiMap.setOnMapClickListener(new OnMapClickCallback() { @Override public void onMapClick(Object object) { new ToastDialog(CommonContext.getContext()).setText("onMapClick ").show(); } }); } }); // Create a Layout object. ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT); PositionLayout myLayout = new PositionLayout(this); myLayout.setLayoutConfig(config); ShapeElement element = new ShapeElement(); element.setShape(ShapeElement.RECTANGLE); element.setRgbColor(new RgbColor(255, 255, 255)); // Load the MapView object. myLayout.addComponent(mapView); super.setUIContent(myLayout); }
  3. Call the onStart(), onActive(), onInactive(), onBackground(), onForeground(), and onStop() methods of MapView in the corresponding methods in MainAbilitySlice.

Running the App

Go to Run > Run ‘module name' in DevEco Studio, click , or press Shift+F10 to run your app. Wait for DevEco Studio to build and install your app on the phone or tablet. The phone or tablet will automatically run your app once the installation is finished.

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 more information about Map Kit, please refer to Map Kit Development Guide.
  3. You can click here to download the source code.
Code copied