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

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202330537081990041&fid=0101187876626530001

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130656.36188624470292877221069723944185:50510904015831:2800:6CEAF5528111206BD7737E3A959BB52104A9A7747E7050BCF00432F1026FE014.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130656.36188624470292877221069723944185:50510904015831:2800:6CEAF5528111206BD7737E3A959BB52104A9A7747E7050BCF00432F1026FE014.png[/IMG]​

Article Introduction

In this article we are going to cover HUAWEI Map Kit JavaScript API introduction. Next we going to implementing HUAWEI Map in Ionic/Cordova project. Lastly we will implement HUAWEI Map Kit JavaScript into Native Application.

Technology Introduction

HUAWEI Map Kit provides JavaScript APIs for you to easily build map apps applicable to browsers.

It provides the basic map display, map interaction, route planning, place search, geocoding, and other functions to meet requirements of most developers.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130733.92848579363294765196283581095813:50510904015831:2800:39FF236C6D6458307AD5B904AB154248A092ECCA5DA31D7A32C79F2FC870076C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130733.92848579363294765196283581095813:50510904015831:2800:39FF236C6D6458307AD5B904AB154248A092ECCA5DA31D7A32C79F2FC870076C.png[/IMG]

Restriction

Before using the service, you need to apply for an API key on the HUAWEI Developers website. For details, please refer to "Creating an API Key" in API Console Operation Guide. To enhance the API key security, you are advised to restrict the API key. You can configure restrictions by app and API on the API key editing page.

Generating API Key

Go to HMS API Services > Credentials and click Create credential.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130805.69497297161968362452366149797133:50510904015831:2800:1A2800A1ACB9CD257BC15013EC342B6CFC8EDEF902F8521050E97BBF8CB8CC98.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130805.69497297161968362452366149797133:50510904015831:2800:1A2800A1ACB9CD257BC15013EC342B6CFC8EDEF902F8521050E97BBF8CB8CC98.png[/IMG]

Click API key to generate new API Key.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130805.70047920776053403472352197556186:50510904015831:2800:ED46316821EC8479A084AB1DB7685AB2DD6197CD5A7E1089AD5B650F250A7EC1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130805.70047920776053403472352197556186:50510904015831:2800:ED46316821EC8479A084AB1DB7685AB2DD6197CD5A7E1089AD5B650F250A7EC1.png[/IMG]

In the dialog box that is displayed, click Restrict to set restrictions on the key to prevent unauthorized use or quota theft. This step is optional.

The restrictions include App restrictions and API restriction.

App restrictions: control which websites or apps can use your key. Set up to one app restriction per key.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130804.18810408994794621698976058478840:50510904015831:2800:5F67D32A4ADC61574B12E9AADB3FD25E83D3B05AE57EE5E4403716C64941D3C7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130804.18810408994794621698976058478840:50510904015831:2800:5F67D32A4ADC61574B12E9AADB3FD25E83D3B05AE57EE5E4403716C64941D3C7.png[/IMG]

API restrictions: specify the enabled APIs that this key can call.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130804.19304458894162560675022938104255:50510904015831:2800:5BB88BA71466F3D79AB82717E1053A8B0119E2DF273591421D796BBD816245BB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130804.19304458894162560675022938104255:50510904015831:2800:5BB88BA71466F3D79AB82717E1053A8B0119E2DF273591421D796BBD816245BB.png[/IMG]

After setup App restriction and API restriction API key will generate.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130805.85202143893764829529807653105477:50510904015831:2800:8B84EDEA2988941532E7A784E3D8CBDF62D6C73AE5F40D5C96D5AD3D6DDD3078.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817130805.85202143893764829529807653105477:50510904015831:2800:8B84EDEA2988941532E7A784E3D8CBDF62D6C73AE5F40D5C96D5AD3D6DDD3078.png[/IMG]

The API key is successfully created. Copy API Key to use in your project.

Huawei Web Map API introduction

1. Make a Basic Map

function loadMapScript() {
const apiKey = encodeURIComponent(
"API_KEY"
);
const src = `https://mapapi.cloud.huawei.com/mapjs/v1/api/js?callback=initMap&key=${apiKey}`;

const mapScript = document.createElement("script");
mapScript.setAttribute("src", src);
document.head.appendChild(mapScript);
}
function initMap() { }

function initMap() {
const mapOptions = {};
mapOptions.center = { lat: 48.856613, lng: 2.352222 };
mapOptions.zoom = 8;
mapOptions.language = "ENG";
const map = new HWMapJsSDK.HWMap(
document.getElementById("map"),
mapOptions
);
}
loadMapScript();

Note: Please update API_KEY with the key which you have generated. In script url we are declaring callback function, which will automatically initiate once Huawei Map Api loaded successfully.

2. Map Interactions

Map Controls

var mapOptions = {};
mapOptions.center = {lat: 48.856613, lng: 2.352222};
mapOptions.zoom = 10;
scaleControl
mapOptions.scaleControl = true; // Set to display the scale.
mapOptions.scaleControlOptions = {
units: "imperial" // Set the scale unit to inch.
};

zoomSlider

mapOptions.zoomSlider = true ; // Set to display the zoom slider.

zoomControl

mapOptions.zoomControl = false; // Set not to display the zoom button.

rotateControl (Manage Compass)

mapOptions.rotateControl = true; // Set to display the compass.

navigationControl

mapOptions.navigationControl = true; // Set to display the pan button.

copyrightControl

mapOptions.copyrightControl = true; // Set to display the copyright information.
mapOptions.copyrightControlOptions = {value: "HUAWEI",} // Set the copyright information.

locationControl

mapOptions.locationControl= true; // Set to display the current location.

Camera

Map moving: You can call the map.panTo(latLng)

Map shift: You can call the map.panBy(x, y)

Zoom: You can use the map.setZoom(zoom) method to set the zoom level of a map.

Area control: You can use map.fitBounds(bounds) to set the map display scope.

Map Events

Map click event:

map.on('click', () => {
map.zoomIn();
});

Map center change event:

map.onCenterChanged(centerChangePost);
function centerChangePost() {
var center = map.getCenter();
alert( 'Lng:'+map.getCenter().lng+'
'+'Lat:'+map.getCenter().lat);
}

Map heading change event:

map.onHeadingChanged(headingChangePost);
function headingChangePost() {
alert('Heading Changed!');
}

Map zoom level change event:

map.onZoomChanged(zoomChangePost);
function zoomChangePost() {
alert('Zoom Changed!')
}

3. Drawing on Map

Marker:

You can add markers to a map to identify locations such as stores and buildings, and provide additional information with information windows.

var map;
var mMarker;

function initMap() {
var mapOptions = {};
mapOptions.center = {lat: 48.856613, lng: 2.352222};
mapOptions.zoom = 8;

map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);

mMarker = new HWMapJsSDK.HWMarker({
map: map,
position: {lat: 48.85, lng: 2.35},
zIndex: 10,
label: 'A',
icon: {
opacity: 0.5
}
});
}

Marker Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.80297041972794614233707520145275:50510904015831:2800:E92BDC0493D82D57B2D2B0B2C9292632EE3CFAA842A2D05B6729508CC0477326.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.80297041972794614233707520145275:50510904015831:2800:E92BDC0493D82D57B2D2B0B2C9292632EE3CFAA842A2D05B6729508CC0477326.png[/IMG]

Marker Clustering:

The HMS Core Map SDK allows you to cluster markers to effectively manage them on the map at different zoom levels. When a user zooms in on the map to a high level, all markers are displayed on the map. When the user zooms out, the markers are clustered on the map for orderly display.

var map;
var markers = [];
var markerCluster;

var locations = [
{lat: 51.5145160, lng: -0.1270060},
{ lat : 51.5064490, lng : -0.1244260 },
{ lat : 51.5097080, lng : -0.1200450 },
{ lat : 51.5090680, lng : -0.1421420 },
{ lat : 51.4976080, lng : -0.1456320 },
···
{ lat : 51.5061590, lng : -0.140280 },
{ lat : 51.5047420, lng : -0.1470490 },
{ lat : 51.5126760, lng : -0.1189760 },
{ lat : 51.5108480, lng : -0.1208480 }
];

function initMap() {
var mapOptions = {};
mapOptions.center = {lat: 48.856613, lng: 2.352222};
mapOptions.zoom = 3;

map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);
generateMarkers(locations);

markerCluster = new HWMapJsSDK.HWMarkerCluster(map, markers);
}

function generateMarkers(locations) {
for (let i = 0; i < locations.length; i++) {
var opts = {
position: locations[i]
};

markers.push(new HWMapJsSDK.HWMarker(opts));
}
}

Cluster markers Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.49370635383875081468288478747064:50510904015831:2800:50AACC8D7983F0277AB82E59510D347773D53A395EAACFFF2622FAA5AA3B01B6.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.49370635383875081468288478747064:50510904015831:2800:50AACC8D7983F0277AB82E59510D347773D53A395EAACFFF2622FAA5AA3B01B6.png[/IMG]

Information Window:

The HMS Core Map SDK supports the display of information windows on the map. There are two types of information windows: One is to display text or image independently, and the other is to display text or image in a popup above a marker. The information window provides details about a marker.

var infoWindow;

function initMap() {
var mapOptions = {};
mapOptions.center = {lat: 48.856613, lng: 2.352222};
mapOptions.zoom = 8;
var map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);
infoWindow = new HWMapJsSDK.HWInfoWindow({
map,
position: {lat: 48.856613, lng: 2.352222},
content: 'This is to show mouse event of another marker',
offset: [0, -40],
});
}

Info window Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.73071891295010212641393305365833:50510904015831:2800:33C89816F201578014910DC1A8DA5DEA1B920848F0E2CDE1F26A7FAB00D0E1D4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.73071891295010212641393305365833:50510904015831:2800:33C89816F201578014910DC1A8DA5DEA1B920848F0E2CDE1F26A7FAB00D0E1D4.png[/IMG]

Ground Overlay

The builder function of GroundOverlay uses the URL, LatLngBounds, and GroundOverlayOptions of an image as the parameters to display the image in a specified area on the map. The sample code is as follows:

var map;
var mGroundOverlay;

function initMap() {
var mapOptions = {};
mapOptions.center = {lat: 48.856613, lng: 2.352222};
mapOptions.zoom = 8;

map = new HWMapJsSDK.HWMap(document.getElementById('map'), mapOptions);

var imageBounds = {
north: 49,
south: 48.5,
east: 2.5,
west: 1.5,
};

mGroundOverlay = new HWMapJsSDK.HWGroundOverlay(
// Path to a local image or URL of an image.
'huawei_logo.png',
imageBounds,
{
map: map,
opacity: 1,
zIndex: 1
}
);
}

Marker Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.29342486526028479726141525482930:50510904015831:2800:2D078A023E30501882602550A6CF28A6ED86858ACBC3D6FF7DCE47484FD814D2.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817132118.29342486526028479726141525482930:50510904015831:2800:2D078A023E30501882602550A6CF28A6ED86858ACBC3D6FF7DCE47484FD814D2.png[/IMG]

Ionic / Cordova Map Implementation

In this part of article we are supposed to add Huawei Map Javascript API’s.

Update Index.html to implment Huawei Map JS scripts:

You need to update src/index.html and include Huawei map javacript cloud script url.

function loadMapScript() {
const apiKey = encodeURIComponent(
"API_KEY"
);
const src = `https://mapapi.cloud.huawei.com/mapjs/v1/api/js?callback=initMap&key=${apiKey}`;

const mapScript = document.createElement("script");
mapScript.setAttribute("src", src);
document.head.appendChild(mapScript);
}
function initMap() { }
loadMapScript();

Make new Map page:

ionic g page maps

Update maps.page.ts file and update typescript:

import { Component, OnInit, ChangeDetectorRef } from "@angular/core";
import { Observable } from "rxjs";

declare var HWMapJsSDK: any;
declare var cordova: any;

@Component({
selector: "app-maps",
templateUrl: "./maps.page.html",
styleUrls: ["./maps.page.scss"],
})
export class MapsPage implements OnInit {
map: any;

baseLat = 24.713552;
baseLng = 46.675297;

ngOnInit() {
this.showMap(his.baseLat, this.baseLng);
}

ionViewWillEnter() {
}

ionViewDidEnter() {
}

showMap(lat = this.baseLat, lng = this.baseLng) {
const mapOptions: any = {};
mapOptions.center = { lat: lat, lng: lng };
mapOptions.zoom = 10;
mapOptions.language = "ENG";
this.map = new HWMapJsSDK.HWMap(document.getElementById("map"), mapOptions);
this.map.setCenter({ lat: lat, lng: lng });
}

}

Ionic / Cordova App Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817133921.50450228781284523250808083749883:50510904015831:2800:451D4FE62447F79BC3DAF1127F7030AE4EFC6F1F563B416E15E7CCD431EFF160.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817133921.50450228781284523250808083749883:50510904015831:2800:451D4FE62447F79BC3DAF1127F7030AE4EFC6F1F563B416E15E7CCD431EFF160.jpg[/IMG]

Native Application Huawei JS API Implementation

In this part of article we are supposed to add javascript based Huawei Map html version into our Native through webview. This part of implementation will be helpful for developer who required very minimal implementation of map.

Make assets/www/map.html file

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817134006.71297616914794328682976602609398:50510904015831:2800:C0E83420B36F7A2C59BBF69011C51C03AC79C31EFAABEC4355750DDB9925C4CF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817134006.71297616914794328682976602609398:50510904015831:2800:C0E83420B36F7A2C59BBF69011C51C03AC79C31EFAABEC4355750DDB9925C4CF.png[/IMG]

Add the following HTML code inside map.html file:

var map;
var mMarker;
var infoWindow;

function initMap() {
const LatLng = { lat: 24.713552, lng: 46.675297 };
const mapOptions = {};
mapOptions.center = LatLng;
mapOptions.zoom = 10;
mapOptions.scaleControl = true;
mapOptions.locationControl= true;
mapOptions.language = "ENG";
map = new HWMapJsSDK.HWMap(
document.getElementById("map"),
mapOptions
);
map.setCenter(LatLng);

mMarker = new HWMapJsSDK.HWMarker({
map: map,
position: LatLng,
zIndex: 10,
label: 'A',
icon: {
opacity: 0.5
}
});
mMarker.addListener('click', () => {
infoWindow.open();
});

infoWindow = new HWMapJsSDK.HWInfoWindow({
map,
position: LatLng,
content: 'This is to info window of marker',
offset: [0, -40],
});
infoWindow.close();
}

Add the webview in your layout:

< WebView
android:id="@+id/webView_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

Update your Activity class to call html file

class MainActivity : AppCompatActivity() {
lateinit var context: Context

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
context = this

val mWebview = findViewById(R.id.webView_map)
mWebview.webChromeClient = WebChromeClient()
mWebview.webViewClient = WebViewClient()
mWebview.settings.javaScriptEnabled = true
mWebview.settings.setAppCacheEnabled(true)
mWebview.settings.mediaPlaybackRequiresUserGesture = true
mWebview.settings.domStorageEnabled = true
mWebview.loadUrl("file:///android_asset/www/map.html")
}
}

Internet permission:

Don’t forget to add internet permissions in androidmanifest.xml file.

< uses-permission android:name="android.permission.INTERNET" />
< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
< uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Native app Result:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817144256.62722470518348663874677080354636:50510904015831:2800:9F5B4BDC0586F30D2605CBF05683BDFC49A4B6B041B2A99A54F8662464C373BA.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/000/584/0890966000000000584.20200817144256.62722470518348663874677080354636:50510904015831:2800:9F5B4BDC0586F30D2605CBF05683BDFC49A4B6B041B2A99A54F8662464C373BA.gif[/IMG]

References:

Huawei Map JavaScript API:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/javascript-api-introduction-0000001050164063

Complete Ionic JS Map Project:

https://github.com/salmanyaqoob/Ionic-All-HMS-Kits

Conclusion

Huawei Map JavaSript Api will be helpful for JavaScript developers to implement Huawei Map on cross platforms like “Cordova, Ionic, React-Native” and also helpful for the Native developers to implement under his projects. Developers can also able to implement Huawei Maps on websites.