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

HMS Location Kit Flutter

Hi everyone , Today I try describing how we can use HMS Location kit Flutter Plugin also I prepare a demo project .

--You can see that at the end of page with Github link.

What is the Location Kit?

HUAWEI Location Kit assists developers in enabling their apps to get quick and accurate user locations and expand global positioning capabilities by using GPS, Wi-Fi, and base station locations.

Fused location: Provides a set of simple and easy-to-use APIs for you to quickly obtain the device location based on the GPS, Wi-Fi, and base station location data.

Activity identification: Identifies user motion status through the acceleration sensor, cellular network information, and magnetometer, helping you adjust your app based on user behavior.

Geofence: Allows you to set an interested area through an API so that your app can receive a notification when a specified action (such as leaving, entering, or lingering in the area) occurs.

If you want to more information about Location kit:

Location Kit

HUAWEI Location Kit combines GPS, Wi-Fi, and base station location data to allow you to quickly obtain precise user…developer.huawei.com

Location Flutter Plugin

The Flutter Location Plugin provides adaption code used for the HUAWEI Location Kit to use in Flutter platform. HUAWEI Location Kit combines the GPS, Wi-Fi, and base station locations to help you quickly obtain precise user locations, build up global positioning capabilities, and reach a wide range of users around the globe.

Configuring Your Flutter Project

Registering as a Developer

Before you get started, you must register as a HUAWEI developer and complete identity verification on the HUAWEI Developers website. For details, please refer to Registration and Verification.

Creating AppGalery Connect Project

Sign in to AppGallery Connect and click My apps.

Click the desired app name.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113247.54744869245770539434688438696112:50511012065647:2800:7E7E45F21BB4A10981D824C733BD8B1D110A6A9AF1EB7EB870171C63663CC1B5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113247.54744869245770539434688438696112:50511012065647:2800:7E7E45F21BB4A10981D824C733BD8B1D110A6A9AF1EB7EB870171C63663CC1B5.png[/IMG]

3. Click the Develop tab.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113307.09609291710346539275344642128392:50511012065647:2800:51239657371755F80CCC0B34EA76C43323DE93E1E926E1B4D936EDBC3DDEC074.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113307.09609291710346539275344642128392:50511012065647:2800:51239657371755F80CCC0B34EA76C43323DE93E1E926E1B4D936EDBC3DDEC074.png[/IMG]

4. In the App information area, click agconnect-services.json to download the configuration file.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113348.38843488618737424490604560988078:50511012065647:2800:46E6398BADD8ACD25EB60FFAFD9720FC1805B77AD2D8E9BD80E495F6B80367B2.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113348.38843488618737424490604560988078:50511012065647:2800:46E6398BADD8ACD25EB60FFAFD9720FC1805B77AD2D8E9BD80E495F6B80367B2.png[/IMG]

If you have made any changes, such as setting the data storage location and enabling or managing APIs, you need to download the latest agconnect-services.json file and use it to replace the existing file in the app directory.

5. Create a Flutter project if you have not created one.

6. Run the following command and make sure that no errors are reported.

[project_path]> flutter doctor

7. Copy the agconnect-services.json file to the android/app directory of your Flutter project.

8. Copy the generated signature file to the android/app directory of your Flutter project.

9. Verify that the agconnect-services.json file and signature file are successfully added to the android/app directory of your Flutter project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113542.32935565329776570674315194857935:50511012065647:2800:7746A01F3E05E20671D3CCC58C697E34FA808EE98C09A92F54106D601CF918BF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113542.32935565329776570674315194857935:50511012065647:2800:7746A01F3E05E20671D3CCC58C697E34FA808EE98C09A92F54106D601CF918BF.png[/IMG]

10. Open the build.gradle file in the android directory of your Flutter project.

a. Go to buildscript, and configure the Maven repository address and AppGallery Connect plugin for the HMS Core SDK.

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

dependencies {
/*
* <Other dependencies>
*/
classpath 'com.huawei.agconnect:agcp:1.2.1.301'
}
}

b. Go to allprojects, and configure the Maven repository address for the HMS Core SDK.

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

11. Open the build.gradle file in the android/app directory of your Flutter project.

a. Add build dependencies.

dependencies {
implementation 'com.huawei.hms:location:4.0.3.301'
/*
* <Other dependencies>
*/
}

b. Add the apply plugin: ‘com.huawei.agconnect’ line after the apply plugin: ‘com.android.application’ line.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

c. Set minSdkVersion to 19 or higher in android > defaultConfig.

defaultConfig {
applicationId "<package_name>"
minSdkVersion 19
/*
* <Other configurations>
*/
}

The value of applicationId must match with that of package_name in the agconnect-services.json file.

d. Configure the signature in android based on the signature file information.

android {
/*
* <Other configurations>
*/
signingConfigs {
release {
storeFile file('<keystore_file>')
storePassword '<keystore_password>'
keyAlias '<key_alias>'
keyPassword '<key_password>'
}
}

buildTypes {
debug {
signingConfig signingConfigs.release
}
release {
signingConfig signingConfigs.release
}
}
}

Replace <keystore_file>, <keystore_password>, <key_alias> and <key_password> with matching entries in your signature file. For details about the app signing procedure in Flutter, please refer to App Signing in Flutter.

Integrating the Plugin

There are two ways to integrate the plugin to your project. Download the Location Kit Flutter Plugin and Using pub.dev HMS location_plugin

Download the Location Kit Flutter Plugin

Download the Location Kit Flutter Plugin and decompress it.

Open the pubspec.yaml file in your Flutter project and add the plugin dependency to the dependencies section.

dependencies:
huawei_location:
path: {library path}

Replace {library path} with the actual library path of the HUAWEI Location Kit Flutter plugin. For details, please refer to Using Packages.

3. Run following command to update the package information:

[project_path]> flutter pub get

4. Run the following command or click the run icon on the toolbar to start the app:

[project_path]> flutter run

Using pub.dev HMS location_plugin

Add this to your package’s pubspec.yaml file:

dependencies:
huawei_location: ^4.0.4+300

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Permissions
First of all we need permissions to access location and physical activity data.

Add location permission to manifest file.Define this permissions in android/app/src/main/AndroidManifest.xml as follows:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113748.51352841558059955772877127115451:50511012065647:2800:EC43815807A91F894DC8879B6A40446B05CA18E4F33D0E1B96EFBFD20E0079BF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/026/400/686/5190090000026400686.20200729113748.51352841558059955772877127115451:50511012065647:2800:EC43815807A91F894DC8879B6A40446B05CA18E4F33D0E1B96EFBFD20E0079BF.png[/IMG]

Create a PermissionHandle intance.

PermissionHandler permissionHandler;

Add initState() for initialize.

permissionHandler = PermissionHandler();

What does the service provide?

Check Permissions

Request Permissions

void requestPermission() async {
try {
bool status = await permissionHandler.requestLocationPermission();
setState(() {
infoText = "Is permission granted $status";
});
} catch (e) {
setState(() {
infoText = e.toString();
});
}
}

Fused Location

Create a FusedLocationProviderClient instance using the init() method and use the instance to call location-related APIs.

FusedLocationProviderClient locationService;

Add initState() for initialize.

locationService = FusedLocationProviderClient();

What does the service provide?

Last Location

Last Location With Address

Mock Location

Location Updates

Reference: Fused Location developer.huawei.com

To use mock location

To use the mock location function, go to Settings > System & updates > Developer options > Select mock location app and select the app for using the mock location function.

(If Developer options is unavailable, go to Settings > About phone and tap Build number for seven consecutive times. Then, Developer options will be displayed on System & updates.)

To use mock location feature first configure the mock location permission in the android/app/src/main/AndroidManifest.xml file.

<uses-permission
android:name="android.permission.ACCESS_MOCK_LOCATION"
tools:ignore="MockLocation,ProtectedPermissions" />

Listen Location Update Event

Call the method onLocationData that listens the location update events.

StreamSubscription<Location> streamSubs;

Add initState()

streamSubs = locationService.onLocationData.listen((location) {
print(location.toString());
});

Example method : getLastLocation()

void getLastLocation() async {
setState(() {
infoText = "";
});
try {
Location location = await locationService.getLastLocation();
setState(() {
infoText = location.toString();
});
} catch (e) {
setState(() {
infoText = e.toString();
});
}
}

Activity Identification

Creating an Activity Identification Service Client

ActivityIdentificationService locationService;

Add initState() for initialize.

locationService = FusedLocationProviderClient();

What does the service provide?

Activity Conversion Updates

Activity Conversion Request

Activity Identification Updates

Activity Identification Request

Listen Activity Identification Event

You can use the onActivityIdentification method to listen to and receive data from activity identification events.

Add initState() for initialize.

ActivityIdentificationService activityIdentificationService =

ActivityIdentificationService();

void onActivityIdentificationResponse(ActivityIdentificationResponse response) {
for (ActivityIdentificationData data
in response.activityIdentificationDatas) {
setChange(data.identificationActivity , data.possibility);
//data.identificationActivity include activity type like vehicle,bike etc.
//data.posibility The confidence for the user to execute the activity.
//The confidence ranges from 0 to 100. A larger value indicates more reliable activity authenticity.
}
}
void streamListen() {
streamSubscription =
activityIdentificationService.onActivityIdentification.listen(onActivityIdentificationResponse);
}

For full content, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201314069177450189&fid=0101187876626530001