Geofencing using Huawei Map, Location and Site Kits for Flutter

Bengisu Özkul
Huawei Developers
Published in
6 min readNov 30, 2020

Hello everyone,

In this article, I am going to use 3 Huawei kits in one project:

  • Map Kit, for personalizing how your map displays and interact with your users, also making location-based services work better for your users.
  • Location Kit, for getting the user’s current location with fused location function, also creating geofences.
  • Site Kit, for searching and exploring the nearby places with their addresses.

What is a Geo-fence?

Geofence literally means a virtual border around a geographic area. Geofencing technology is the name of the technology used to trigger an automatic alert when an active device enters a defined geographic area (geofence).

As technology developed, brands started to reach customers. Of course, at this point, with digital developments, multiple new marketing terms started to emerge. Geofencing, a new term that emerged with this development, entered the lives of marketers.

Project Setup

HMS Integration

Firstly, you need a Huawei Developer account and add an app in Projects in AppGallery Connect console. So that you can activate the Map, Location and Site kits and use them in your app. If you don’t have an Huawei Developer account and don’t know the steps please follow the links below.

Important: While adding app, the package name you enter should be the same as your Flutter project’s package name.

Note: Before you install agconnect-services.json file, make sure the required kits are enabled.

Enabling required APIs

Permissions

In order to make your kits work perfectly, you need to add the permissions below in AndroidManifest.xml file.

Creating Flutter Application

Add Dependencies to ‘pubspec.yaml’

After completing all the steps above, you need to add the required kits’ Flutter plugins as dependencies to pubspec.yaml file. You can find all the plugins in pub.dev with the latest versions. You can follow the steps in installing section of the following links.

After adding them, run flutter pub get command.

All the plugins are ready to use!

Request Location Permission and Get Current Location

Create a PermissionHandler instance and initialize it in initState to ask for permission. Also, follow the same steps for FusedLocationProviderClient. With locationService object, we can get the user’s current location by calling getLastLocation() method.

In requestPermission() method, you can find both Location Permission and Background Location Permission.

When you launch the app for the first time, the location permission screen will appear.

Add HuaweiMap

Huawei Map is the main layout of this project. It will cover all the screen and also we will add some buttons on it, so we should put HuaweiMap and other widgets into a Stack widget. Do not forget to create a Huawei map controller.

We have got the current location with Location service’s getLastLocation() method and assigned it to center variables as longitude and latitude. While creating the HuaweiMap widget, assign that center variable to HuaweiMap’s target property, so that the app opens with a map showing the user’s current location.

When onClick method of HuaweiMap is triggered, call placeSearch using the Site Kit’s nearbySearch method. Thus, you will get a Site object to assign to the new geofence you will add.

Create Geofence

When the user touch somewhere on the map; a marker, a circle around the marker, a Slider widget to adjust the radius of the circle, and a button named “Add Geofence” will show up on the screen. So we will use a boolean variable called clicked and if it’s true, the widgets I have mentioned in the last sentence will be shown.

Create a Slider widget wrapped with a Positioned widget and put them into Stack widget as shown below.

- Adjusting the radius of the geofence -

After implementing addMarker and drawCircle methods and adding Slider widget, now we will create AddGeofence Screen and it will appear as a ModalBottomSheet when AddGeofence button is clicked.

In the new stateful AddGeofenceScreen widget’s state class, create GeofenceService and SearchService instances and initialize them in initState.

To monitor address, radius and also to select conversion type of the geofence, we will show a ModalBottomSheet with the widgets shown below.

For each conversion type, add a RadioListTile widget.

When you click SAVE button, addGeofence method will be called to add new Geofence to the list of Geofences, then return to the Home screen with false value to update clicked variable.

In addGeofence, do not forget to call createGeofenceList method with the list you have just added the geofence in.

To listen to the geofence events, you need to use onGeofenceData method in your code.

- Adding geofence -

Search Nearby Places

In home screen, place a button onto the map to search nearby places with a keyword and when it is clicked a new alertDialog page will show up.

After you enter the keyword and click Search button, there will be markers related to the keyword will appear on the map.

- Searching nearby places -

You can find the full code in my github page. Here is the link for you.

Conclusion

In this article you have learnt how to use some of the features of Huawei Map, Location and Site kits in your projects. Also, you have learnt the geofencing concept. Now you can add geofences to your app and with geofencing, you can define an audience based on a customer’s behavior in a specific location. With location information, you can show suitable ads to the right people simultaneously, wherever they are.

Thank you for reading this article, I hope it was useful and you enjoyed it!

--

--