[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Original link of this thread: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201320049432220250&fid=0101187876626530001

Huawei Map Kit, How to setup Huawei Map and add a marker? In JAVA

HUAWEI Map Kit is an SDK for map development. It covers map data of more than 200 countries and regions, and supports dozens of languages. With this SDK, you can easily integrate map-based functions into your apps.

In this article we will going to talk about How to setup Huawei Map and add a marker:

First you need to add Dependencies in root build.gradle:

allprojects {
repositories {
maven { url 'https://developer.huawei.com/repo/' }
}
}

buildscript {
repositories {
maven { url 'http://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.huawei.agconnect:agcp:1.2.0.300'
}
}

Then you need to add Dependencies in app build.gradle:

implementation 'com.huawei.hms:maps:5.0.1.300'

Make sure that you configure

apply plugin: 'com.huawei.agconnect'
after apply plugin: 'com.android.application'.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/686/864/2640027000000686864.20200805095302.37008863439616202786024818153662:50510820054834:2800:31F5952BA48D45137F547F317A5CBB06D8C5623F1045165B6B2163BDDB3CFC26.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/686/864/2640027000000686864.20200805095302.37008863439616202786024818153662:50510820054834:2800:31F5952BA48D45137F547F317A5CBB06D8C5623F1045165B6B2163BDDB3CFC26.png[/IMG]

Also check on AppGallery Connect as picture below on Project Setting > Manage APIs. Map Kit switched on.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/686/864/2640027000000686864.20200805094928.48528737403338228240837912520192:50510820054834:2800:EF23EEC2AA0A8C463E9FA326E878C6AF570490DA56416F930DBFA899F35496C1.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/686/864/2640027000000686864.20200805094928.48528737403338228240837912520192:50510820054834:2800:EF23EEC2AA0A8C463E9FA326E878C6AF570490DA56416F930DBFA899F35496C1.jpg[/IMG]

Also don’t forget on AppGallery Connect as picture below on Project Setting to add your SHA-256 certificate fingerprint for the project and download agconnect-services.json

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/686/864/2640027000000686864.20200805095022.68545234109783025441042288442813:50510820054834:2800:E817A34C61BD4C6DBCD63BC36DA1FAF3E658331031569F6D40F3D58F25B0A2A1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/686/864/2640027000000686864.20200805095022.68545234109783025441042288442813:50510820054834:2800:E817A34C61BD4C6DBCD63BC36DA1FAF3E658331031569F6D40F3D58F25B0A2A1.png[/IMG]

Also don’t forget to add the permissions on AndroidManifest.xml

<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"/>

Remember that fingerprint for debugging is different than release so you need to use one for both so you can use this by this way. Add this code in app Gradle and change it to your keystore the released one.

android {
…

signingConfigs {
release {
storeFile file("../keystore/HMS_MY_TEST_KEYSTORE.jks") //keystore path
storePassword "@123456"
keyAlias "HMS_MY_TEST_KEYSTORE"
keyPassword "@123456"
v1SigningEnabled true
v2SigningEnabled true
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
…
}

and ofcourse don't forget to check permissions to let the user allow App to access user location from android 6.0

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
Log.i(TAG, "sdk > 23 M");
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] strings =
{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions(this, strings, 882);
}
}

In you activity you need to handle the response by this way

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "onRequestPermissionsResult: apply LOCATION PERMISSION successful");
requestLocation();
} else {
Log.i(TAG, "onRequestPermissionsResult: apply LOCATION PERMISSSION failed");
}
}
}

Now Let's start with a real work:

First you have to add MapView to the layout. \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>

Now yon need to initialize MapView:

// get mapView by layout view
mMapView = findViewById(R.id.mapView);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}
mMapView.onCreate(mapViewBundle);

// get map by async method
mMapView.getMapAsync(this);

Don't forget to integrate map with activity cycle:

@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();
}

Finally you have to create call back when map is ready

@Override
public void onMapReady(HuaweiMap huaweiMap) {
hmap = huaweiMap;
hmap.setMyLocationEnabled(true);
hmap.getUiSettings().setMyLocationButtonEnabled(true);
}

Inside it also you can add some settings and configrations for map.

Now we will talk about adding marker on map:

and it's so simple:

To add marker on map you can added by this way

public void addMarker() {
if (null != mMarker) {
mMarker.remove();
}
MarkerOptions options = new MarkerOptions()
.position(new LatLng(48.893478, 2.334595))
.title("Hello Huawei Map")
.snippet("This is a snippet!");
mMarker = hmap.addMarker(options);
}

and you can call this function from onMapReady.

Thanks a lot for reading.