👨🏼‍💻Develop Your Bicycle Rental App in 6 Easy Steps | Part-I

Healthy Cycling App: Auth Service, Map Kit, Location Kit

Feyza Ürkut
Huawei Developers
6 min readJan 17, 2023

--

Introduction

Hello everyone💥

In this series of articles, I will explain you how we can develop a Bike Rental Application in 6 easy steps using Huawei Services. In the first part of the series, I will talk about how we use the Auth Service, Location Kit and Map Kit, which form the basis of our application. Let’s start with the authentication and map operations steps!

Content

Introducing the Healthy Cycling App

1- Integrating HUAWEI ID sign-in with Auth Service

2- Add Dependencies & Permissions for Map and Location Kits

3- Using Map Kit to Draw Route and Show Suitable Bicycle

4- Using Location Kit to Get Current Location and Listen Location in Background

Healthy Cycling App

The Healthy Cycling is a sample application with which you can perform bicycle rental operations and see your weekly progress with some data like total distance, time, calorie etc.

With this application, I aim to encourage people to use bicycles that are healthy and environmentally friendly.

The mix of bicycle rental and exercise application. Reduce air pollution and live healthily!

Some of the HMS Kits and Services that we use in our project, which are among the capabilities provided by HUAWEI Mobile Services for efficient development, are as follows:

  • Auth Service
  • Map Kit
  • Location Kit
  • Scan Kit
  • IAP
  • Health Kit
  • Push Kit
  • App Messaging

The Android technologies we use in this project, which we are progressing with MVVM architecture, single activity multiple fragments approach and clean code principles, are as follows:

  • Firebase Firestore
  • View Binding
  • Dependency Injection — Hilt
  • Navigation Component
  • Coroutines
  • State Flow
  • Service
  • Lottie Animation
  • Glide

1- Integrating HUAWEI ID Sign-In with Auth Service

Huawei Auth Service supports multiple authentication modes, and provides a powerful management console, enabling you to easily develop and manage user authentication.
The sample code in this project uses the HUAWEI ID sign-in mode for using Huawei Health Kit. Therefore, you need to enable the HUAWEI ID authentication mode of Huawei Auth Service in App Gallery Connect.

You can follow these steps in detail from my previous article, where I mentioned the integration of Huawei Auth Service and the Activity Result Contracts that we will use instead of the deprecated startActivityForResult() function.

2- Add Dependencies & Permissions for Map and Location Kits

  1. Create an app in App Gallery Connect and integrate the App Gallery Connect SDK into your app. For details, you can follow the preparations codelab: Preparations for Integrating HUAWEI HMS Core

2. Add build dependencies to the dependencies block.

app-level gradle file

3. Add permissions to the AndroidManifest.xml file.

4. Apply for related permissions. In Android 6.0 and later, you need to apply for location permissions dynamically.

3- Using Map Kit to Draw Route and Show Suitable Bicycle

Map Kit provides powerful and convenient map services for you to easily implement personalized map viewing and interaction.

In our project, on the main page, we show the user’s location and the suitable bicycles located nearby on a map. When you complete the ride, we make you draw the route of your ride on the payment page we direct you to. Let’s let’s start coding!

Create a Map Instance Using MapView

  1. Add MapView to home fragment layout file.

2. Initialize the map in MainActivity.kt.

3. We implement the OnMapReadyCallback API to use map in home fragment.

4. Call the onMapReady() callback to obtain the HuaweiMap object and enable the user’s current location.

5. We call getMapAsync() to register a callback.

6. Set a lifecycle listener for the MapView and call onSaveInstanceState(Bundle outState) method.

Add Markers for Suitable Bikes

We kept the information of our bicycles in the Firebase database. After pulling our data in a list from this database, we showed our non-rented bicycles on the map using the location parameter.

  1. If the bikeList with id, isRented, location parameters are not empty, we can create a marker for the non-rented bike. Use the default icon to add markers to the map.

After Riding Draw Route

  1. Add a polyline using the list of locations we have.

4- Using Location Kit to Get Current Location and Listen Location in Background

Location Kit provides capabilities for you to obtain the precise user device location quickly, helping you build up global positioning capabilities and expand your global business.

In our project, we take the user’s current location using the Location Kit and focus the map’s camera there. In addition to this process, we perform our location listening process in the background, which is one of the main functions of the project, using the Location Kit and Service structure.

Get the User’s Current Location

  1. Define each necessary instance in module class.

2. Call checkLocationSettings() to check the device location settings. You can call the getLastLocation() method to obtain the last known location of a device.

3. Move the map camera to focus the right place on the map using the user’s last location.

Listen Location in Background

  1. Create an interface for abstracting the location updates.

2. Call onLocationResult() method to send location result when our client (FusedLocationProviderClient) receives a new location.

3. Create LocationService class and don’t forget to register the service in manifest file. In the service class, we create two method to manage foreground services; start() and stop(). In start() function, we get location updates every 10 sec and notify the user.

4. Finally, create an intent to start or stop the service by changing the action parameter.

Conclusion

We have completed the first article of the series here🎉I tried to tell you about the Map Kit and Location Kit as I used them in the project. You can also easily use these kits for different use cases while developing your application. I hope it was a useful article!

See you in my next article that I will explain the Scan Kit and In-App Purchases processes that we use the Activity Result APIs to get results from the activity👩‍💻😊

References

--

--