HMS Location Kit Flutter

Rahmican İşin
Huawei Developers
Published in
7 min readJul 28, 2020

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

  1. Sign in to AppGallery Connect and click My apps.
  2. Click the desired app name.

3. Click the Develop tab.

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

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.

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

  1. Download the Location Kit Flutter Plugin and decompress it.
  2. 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:

Create a PermissionHandle intance.

PermissionHandler permissionHandler;

Add initState() for initialize.

permissionHandler = PermissionHandler();

What does the service provide?

  • Check Permissions
  • Request Permissions

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

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()

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

We use ActivityIdentificationData for detecting the activity.

ActivityIdentificationData

Example method : createActivityIdentificationUpdates()

Geofence Service

Creating an Activity Identification Service Client

GeofenceService geofenceService;

Add initState() for initialize.

geofenceService = GeofenceService();

What does the service provide?

  • Activity Conversion Updates
  • Activity Conversion Request
  • Activity Identification Updates
  • Activity Identification Request

Listen Geofence Events

You can use onGeofenceData method to listen geofence events.

StreamSubscription<GeofenceData> geofenceStreamSub;

Add initState()

geofenceStreamSub = geofenceService.onGeofenceData.listen((data) {
print(data.toString);

});

Example metod: addGeofence()

Fused Location Service
Activity Identification Service
Geofence Service

Full code

In this article, you learned how implement Location Kit with Flutter.

I hope it was useful. Please feel free to ask questions.

--

--