Intermediate: Obtaining Device Location in Harmony OS

Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover Device Location with Java in Harmony OS.

The Location service provides user current device location. HarmonyOS, mobile devices can obtain accurate real-time location or last known location of a mobile device. Mobile devices plays an important role in people’s daily routines, whether it can be for looking at the weather forecast, truck routing, local news, navigating, historical traffic patterns and beyond. All these activities are so much associated with the location services on mobile devices.

Limitations and Constraints

Your application can use the location function only after the user has granted location permission. If user doesn’t provide location permission, then the system will not provide the location service for any application.

When to Use

To obtain the real-time location or last known location of a mobile device can call location-related APIs in HarmonyOS. If you want lower power consumption when the real-time location of the device is not needed, you may consider obtaining the last known location of the device.

Development Overview

You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.
  • A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK installation package.
  • DevEcho studio installed.

Follows the steps.

1. Create Unity Project.

  • Open DevEcho studio.
  • Click NEW Project, select a Project Templet.
  • Select ability template and click Next as per below image.
  • Enter Project and Package Name and click on Finish.

2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.

3. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values. For Accessing location service define below permission in config.json file.

4. Create New Ability as follows

5. Development Procedure.

Create MainAbility.java ability and add the below code.

Create ability_main.xml layout and add the below code.

Create background_button_green_color.xml in graphic folder and add the below code.

6. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.

Result

  1. Run Application on connected device and provide respective permission as per below image.

2. Click on “Get Current Location” button for fetching current location as per below screen.

3. Click on “Get Continuous Location” button, for fetching continuous locations as per pre define interval or distance per below images.

4. Click on “Stop access location” to stop access location as per below images.

Tips and Tricks

  • Always use the latest version of DevEcho Studio.
  • Use Harmony Device Simulator from HVD section.
  • Do not forgot to add permission in config.json file.

Conclusion

In this article, we have learnt Device Location in Harmony OS. Location related API in harmonyOS we can access user device real time location. If you want lower power consumption when the real-time location of the device is not needed, you may consider obtaining the last known location of the device user.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1

Location Overview: https://developer.harmonyos.com/en/docs/documentation/doc-guides/device-location-overview-0000000000031896?ha_source=hms1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store