目录

1 Overview

This tutorial helps you migrate the Google Map SDK for Android to the HMS Core Map SDK for Android in your Android app. It starts with basic environment settings, and covers some basic use cases, such as creating a map and adding a marker. At the bottom of the page is a summary in which we provide links to an automated tool that helps you finish the migration.

2 Preparations

Before integrating HMS Core Map Kit, complete the following preparations:

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

3 Creating a Map

This section describes how to create a map using Google Map SDK and HMS Core Map Kit.

3.1 Google Map SDK

1. Add build dependencies to the build.gradle file of your app.

  1. implementation 'com.google.android.gms:play-services-maps:15.0.0'

2. Add a fragment to the activity layout file.

  1. <fragment
  2.   android:id="@+id/mapFragment"
  3.   android:name="com.google.android.gms.maps.MapFragment"
  4.   android:layout_width="match_parent"
  5.   android:layout_height="match_parent"/>

3. Add a Google Android API key in the application section in the manifest.xml file.

  1. <meta-data
  2.  android:name="com.google.android.geo.API_KEY"
  3.  android:value="YOUR_GOOGLE_KEY_GOES_HERE"/>

3.2 Map Kit

1. Add build dependencies to the build.gradle file of your app.

  1. implementation 'com.huawei.hms:maps:4.0.1.301'

2. Add a fragment to the activity layout file.

  1. <fragment
  2.   xmlns:map="http://schemas.android.com/apk/res-auto"
  3.   android:id="@+id/mapfragment_mapfragmentdemo"
  4.   class="com.huawei.hms.maps.MapFragment"
  5.   android:layout_width="match_parent"
  6.   android:layout_height="match_parent"
  7.   map:cameraTargetLat="48"
  8.   map:cameraTargetLng="118"/>

4 Adding a Marker

This section describes how to add a marker using Google Map SDK and HMS Core Map Kit.

4.1 Google Map SDK

1. Define a GoogleMap object and a MapFragment object.

  1. private GoogleMap gMap;
  2. private MapFragment mapFragment;

2. Implement the OnMapReadyCallback API and override the onMapReady method.

  1. @Override
  2. public void onMapReady(GoogleMap googleMap) {
  3.   gMap = googleMap;
  4.   LatLng amsterdam = new LatLng(52.37, 4.90);
  5.   gMap.addMarker(new MarkerOptions().position(amsterdam).title("Amsterdam"));
  6.   gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(amsterdam, 8));
  7. }

3. Load MapFragment in the onCreate method of the activity and call getMapAsync().

  1. mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.mapFragment);
  2. mapFragment.getMapAsync(this);

4.2 Map Kit

1. Define a HuaweiMap object and a MapFragment object.

  1. private HuaweiMap hMap;
  2. private MapFragment mMapFragment;

2. Implement the OnMapReadyCallback API and override the onMapReady method.

  1. @Override
  2. public void onMapReady(HuaweiMap map) {
  3. // after call getMapAsync method ,we can get HuaweiMap instance in this callback method
  4.   hMap = map;
  5.   hMap.addMarker(new MarkerOptions().position(new LatLng(31.2304, 121.4737)).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_background)));
  6.   hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(31.2304, 121.4737), 10));
  7. }

3. Load MapFragment in the onCreate method of the activity and call getMapAsync().

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3.    super.onCreate(savedInstanceState);
  4.    setContentView(R.layout.activity_map_fragment_demo);
  5.    mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapfragment_mapfragmentdemo);
  6.    mMapFragment.getMapAsync(this);
  7. }
Notice:
To replace the Google Map SDK for Android of the V2 version with the HMS Core Map Kit, you need to replace the package name com.google.android.gms.mapswithcom.huawei.hms.maps. To replace the Google Map SDK for Android of the V3 version with the HMS Core Map Kit, you need to replace the package name com.google.android.libraries.maps with com.huawei.hms.maps. (To replace the package name, you can go to Edit>Find>Replace in Path in Android Studio.)


5 Summary

This tutorial has covered the differences between the Google Map SDK and HMS Core Map Kit. HMS ToolKit, an automated tool, is also provided to help you migrate from the Google Map SDK to HMS Core Map Kit or add HMS Core Map Kit to your app efficiently. For details, visit the following links:

HMS ToolKit - Overview

Map Kit Manual Conversion Guide