location-service:基于HarmonyOS的位置服务应用项目

用户可用于查看城市天气、出行打车、旅行导航及运动轨迹观察等场景。项目通过LocationKit实现缓存位置、当前位置获取和持续定位,结合BackgroundTasksKit实现后台定位,并在地图标记位置信息。

Branch4Tags0

Obtaining Device Location Information Based on the Location Service

Overview

This sample demonstrates how to use geoLocationManager of @kit.LocationKit to retrieve cached locations and the current location, as well as enable continuous positioning. It also uses backgroundTaskManager of @kit.BackgroundTasksKit to start continuous tasks for background positioning functionality. Additionally, this sample uses map.Marker to mark locations on a map. You can integrate this sample code to implement positioning capabilities in development scenarios that require device location information, such as local weather inquiry, ride hailing, travel navigation, and movement path tracking.

Preview

Home

How to Use

Before using the sample code, complete the basic preparations and public key fingerprint configuration by referring to Application Development Preparations, enable Map Kit by referring to section "Configuring App Information in AppGallery Connect" in the Map Kit development guide, and manually sign the application by referring to the corresponding section of the application/atomic service.

  1. On the home page, tap the Obtain Cached Location button to obtain the cached location information and mark it on the map.
  2. On the home page, tap the Obtain Current Location button to obtain the current location information and mark it on the map.
  3. On the home page, tap the Continuous Location button. The location information is displayed on the map and is continuously updated. Tap the Continuous Location button again to stop the continuous location update.
  4. On the home page, tap the Background Location button. The location information is displayed on the map. After the application transitions to the background, the location information can still be updated. After the application returns to the foreground, when you tap the Background Location button again, the application will transition to the background and the location information will not be updated. Note: If the phone remains absolutely still for a long time, the resource management will freeze the application. The system stops positioning for the frozen applications. After the application is unfrozen, positioning is restored. Unfreezing measures: 1. Bring the application back to the foreground. 2. Move the phone to exit the still state. (If the positioning function does not immediately resume, the movement may not be sufficient for the system to detect the phone movement or the location change. There may be a delay in unfreezing the application when the screen is off, which can cause a delay in restoring the positioning function.)

Project Directory

├──entry/src/main/ets/                    // Code
│  ├──common
│  │  └──CommonConstants.ets              // Common constants
│  ├──entryability
│  │  └──EntryAbility.ets                 // Entry ability
│  ├──model
│  │  └──LocationInter.ets                // Location latitude and longitude information
│  └──pages
│     └──Index.ets                        // Home page
└──entry/src/main/resources               // Static resources

How to Implement

  • When the user taps the Obtain Cached Location button, call the geoLocationManager.getLastLocation API to obtain the cached location, call the Marker API to mark the location on the map, and call the geoLocationManager.getAddressesFromLocation API to convert coordinates into geographical location information.
  • When the user taps the Obtain Current Location button, call the geoLocationManager.getCurrentLocation API to obtain the current location, call the Marker API to mark the location on the map, and call the geoLocationManager.getAddressesFromLocation API to convert coordinates into geographical location information.
  • When the user taps the Continuous Location button, call the geoLocationManager.on API to continuously obtain the location information by listening to the longitude and latitude changes, call the Marker API to mark the most recently updated location on the map, and call the geoLocationManager.getAddressesFromLocation API to convert coordinates into geographical location information.
  • When the user taps the Background Location button, call the backgroundTaskManager.startBackgroundRunning API to start continuous tasks for background positioning functionality, and call the geoLocationManager.on API to enable listening for longitude and latitude changes, so that the location information can be continuously obtained when the application transitions to the background. Call the Marker API to mark the location on the map. Call the geoLocationManager.getAddressesFromLocation API to convert coordinates into geographical location information.

Required Permissions

ohos.permission.LOCATION: allows an application to obtain the device location.

ohos.permission.APPROXIMATELY_LOCATION: allows an application to obtain the approximate location of a device.

ohos.permission.LOCATION_IN_BACKGROUND: allows an application running in the background to obtain the device location.

ohos.permission.KEEP_BACKGROUND_RUNNING: allows an application to start continuous tasks.

ohos.permission.GET_NETWORK_INFO: allows an application to obtain the network connection information of a device. Note: This permission is not required for obtaining the device location information. In this example, this permission is requested to detect the network connection status of the device and provide appropriate prompts when there is no network connection.

Dependencies

N/A

Constraints

  1. This sample is only supported on Huawei phones running standard systems.
  2. The HarmonyOS version must be HarmonyOS 5.0.5 Release or later.
  3. The DevEco Studio version must be DevEco Studio 5.0.5 Release or later.
  4. The HarmonyOS SDK version must be HarmonyOS 5.0.5 Release SDK or later.

Introduction

用户可用于查看城市天气、出行打车、旅行导航及运动轨迹观察等场景。项目通过LocationKit实现缓存位置、当前位置获取和持续定位,结合BackgroundTasksKit实现后台定位,并在地图标记位置信息。

Customize your domain