# Geolocation
# Interface Declaration
{ "name": "system.geolocation" }
# Import Module
import geolocation from '@system.geolocation'
// or
const geolocation = require('@system.geolocation')
# Interface Definition
# geolocation.getLocation(OBJECT)
Obtain geolocation
# Permission Requirements
Precise device positioning
Developers need to configure permissions in manifest.json:
{
"permissions": [
{ "name": "hapjs.permission.LOCATION" }
]
}
# Parameters:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
| timeout | Number | No | Set timeout in milliseconds. The default value is 30000 |
| success | Function | Yes | Callback for success |
| fail | Function | No | Callback for failure, possibly due to lack of permissions |
| complete | Function | No | Callback after execution is complete |
# Return Values for success:
| Parameter Name | Type | Description |
|---|---|---|
| longitude | Number | Longitude, floating-point number |
| latitude | Number | Latitude, floating-point number |
| altitude | Number | Altitude/height in meters, floating-point number |
| speed | Number | Speed in meters per second, floating-point number |
| accuracy | Number | Accuracy, positive integer value |
| accuracyInfo | { horizontal: Number, vertical: Number } | Accuracy information, including horizontal and vertical accuracy |
# Error Codes Returned by fail:
| Error Code | Description |
|---|---|
| 203 | Function not supported |
| 204 | Timeout occurred |
# Example:
geolocation.getLocation({
success: function(data) {
console.log(
`handling success: longitude = ${data.longitude}, latitude = ${
data.latitude
}, speed = ${data.speed}, altitude = ${data.altitude}`
)
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}, errorMsg=${data}`)
}
})
# geolocation.subscribe(OBJECT)
Listen for geolocation changes. If called multiple times, only the last call will take effect.
# Permission Requirements
Precise device positioning
Developers need to configure permissions in manifest.json:
{
"permissions": [
{ "name": "hapjs.permission.LOCATION" }
]
}
# Parameters:
| Parameter Name | Type | Required | Description |
|---|---|---|---|
| callback | Function | Yes | Callback triggered each time the location information changes |
| fail | Function | No | Callback for failure |
# Return Values for callback:
| Parameter Name | Type | Description |
|---|---|---|
| longitude | Number | Longitude, floating-point number |
| latitude | Number | Latitude, floating-point number |
| altitude | Number | Altitude/height in meters, floating-point number |
| speed | Number | Speed in meters per second, floating-point number |
| accuracy | Number | Accuracy, positive integer value |
# Error Codes Returned by fail:
| Error Code | Description |
|---|---|
| 203 | Function not supported |
# Example:
geolocation.subscribe({
callback: function(data) {
console.log(
`handling success: longitude = ${data.longitude}, latitude = ${
data.latitude
}, speed = ${data.speed}, altitude = ${data.altitude}`
)
},
fail: function(data, code) {
console.log(`handling fail, code = ${code}, errorMsg=${data}`)
}
})
# geolocation.unsubscribe()
Cancel listening for geolocation changes
# Permission Requirements
Precise device positioning
Developers need to configure permissions in manifest.json:
{
"permissions": [
{ "name": "hapjs.permission.LOCATION" }
]
}
# Parameters:
None
# Example:
geolocation.unsubscribe()
# Support Details
| Device Product | Description |
|---|---|
| Xiaomi S1 Pro Sports & Health Watch | Not supported |
| Xiaomi Smart Band 8 Pro | Not supported |
| Xiaomi Smart Band 9 / 9 Pro | Not supported |
| Xiaomi Watch S3 | Supported |
| Redmi Watch 4 | Not supported |
| Xiaomi Wrist ECG Blood Pressure Monitor | Not supported |
| Xiaomi Smart Band 10 | Not supported |
| Xiaomi Watch S4 | Supported |
| REDMI Watch 5 | Supported |