[Huawei Developers] Freemind R:
Introduction

One of the unique features of mobile applications is location awareness. Mobile users take their devices with them everywhere, and adding location awareness to our app offers users a more contextual experience. The Huawei Location kit facilitate adding location awareness to our app with automated location tracking, geofencing, and activity recognition/identification.

HMS Location Kit Services

1. Fused Location

2. Activity Identification

3. Geofence

Ionic Framework

Ionic Framework is an open source UI toolkit for building performant, high-quality mobile and desktop apps using web technologies such as HTML, CSS, and JavaScript with integrations for popular frameworks like Angular and React.

Think of Ionic as the front-end UI framework that handles all of the look and feel and UI interactions your app needs in order to be compelling. Unlike a responsive framework, Ionic comes with very native-styled mobile UI elements and layouts that you should get with a native SDK on Android or iOS but didn’t really exist before on the web.

Since Ionic is an HTML5 framework, it needs a native wrapper like Cordova or Capacitor in order to run as a native app.

Here we will use Ionic framework with Angular and Capacitor as native wrapper.

Demo

Prerequisite

1) Must have a Huawei Developer Account.

2) Must have a Huawei phone with HMS 4.0.0.300 or later.

3) Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

4) Must install node in the system

5) Must install Ionic in the system using below command:

1	npm install -g @iONiC/cli

Things need to be done

1) Generating a Signing Certificate Fingerprint. For generating the SHA key, refer this article.

2) Create an app in the Huawei AppGallery connect and enable Location Kit in Manage Api section.

3) Provide the SHA Key in App Information Section.

4) Provide storage location.

5) Download the agconnect-services.json and store it somewhere on our computer.

6) Create a blank Ionic Project using below command:

ionic start Your_Application_Name blank --type=angular

7) Run the following command in the root directory of your Ionic project to install HMS Location kit it through npm.

npm install @hmscore/cordova-plugin-hms-location

8) If you want full Ionic support with code completion etc., install @iONiC-native/core in your project.

npm install @ionic-native/core --save-dev

9) Run the following command to copy the "ionic/dist/hms-location" folder from library to "node_modules/@iONiC-native" folder under your Ionic project.

cp node_modules/@hmscore/cordova-plugin-hms-location/ionic/dist/hms-location node_modules/@ionic-native/ -r

10) Run the following command to compile the project:

ionic build

npx cap init [appName] [appId]

Where appName is the name of your app, and appId is package_name in your agconnect-services.json file (ex: com.example.app).

11) Run the following command to add android platform to your project:

ionic capacitor add android

12) Make sure your project has a build.gradle file with a maven repository address and agconnect service dependencies as shown below:

buildscript {

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

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.google.gms:google-services:4.3.3'
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

apply from: "variables.gradle"

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

}

13) Add the Signing certificate configuration to the build.gradle file in the app directory as show below:

android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "YOUR_PACKAGE_NAME"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file("mykeystore.jks") // Signing certificate.
storePassword "XXXXXX" // KeyStore password.
keyAlias "XXXXXX" // Alias.
keyPassword "XXXXXX" // Key password.
v1SigningEnabled true
v2SigningEnabled true
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.release
debuggable true
}
}
}

14) Add plugin to the build.gradle file in the app directory as show below:

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

15) Add Location Kit service implementation into to dependencies section of build.gradle file in the app directory as show below:

dependencies {
…
implementation 'com.huawei.hms:location:5.0.4.300'
}

16) Add agconnect-services.json and signing certificate jks file to the app directory in your Android project as show below:

17) To update dependencies, and copy any web assets to your project, run the following code:

npx capacitor sync

Intialize Location

We need to initialize location, fused location and activity identification as shown below:

ngOnInit() {
this.hmsLocationKit.init();
this.hmsFusedLocation.init();
this.hmsActivityIdentification.init();
}

ngOnInit() function is a life cycle hook called by Angular2 to indicate that Angular is done creating the component.

Permission Needed

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="com.huawei.hms.permission.ACTIVITY_RECOGNITION"/>

Fused Location

It helps us to obtain device location based on Global Navigation Satellite System, Wi-Fi, and base station location data.

Check For Fused Location Permissions

In order to check fused location permission is there or not, we need to call hasPermission() as shown below:

async checkLocationPermission() {
var res = await this.hmsFusedLocation.hasPermission();
alert(" HAS PERMISSION : "+res.hasPermission);
}

More details, you can visit https://forums.developer.huawei.com/forumPortal/en/topic/0203412577215750222