Geolocation
Start from API_LEVEL
2.1
. Please refer to API_LEVEL.
Geolocation Sensor.
info
permission code: device:os.geolocation
Methods
start
Start listening to location data
start(): void
stop
Stop listening to location data
stop(): void
getStatus
Get the positioning status, return A
for positioning in progress, return V
for invalid positioning
getStatus(): string
getLatitude
Get Latitude
getLatitude(option: Option): Result
Option
Property | Type | Required | DefaultValue | Description | API_LEVEL |
---|---|---|---|---|---|
format | string | N | DMS | Coordinate format, optionally DD for decimal or DMS in degrees, minutes and seconds | 2.1 |
Result
Type | Description |
---|---|
number|DMS | Coordinates, coordinate system type WGS-84 |
DMS
Property | Type | Description | API_LEVEL |
---|---|---|---|
direction | string | Direction, N for north latitude, S for south latitude | 2.1 |
degrees | number | degree | 2.1 |
minutes | number | minute | 2.1 |
seconds | number | second | 2.1 |
getLongitude
Get Longitude
getLongitude(option: Option): Result
Option
Property | Type | Required | DefaultValue | Description | API_LEVEL |
---|---|---|---|---|---|
format | string | N | DMS | Coordinate format, optionally DD for decimal or DMS in degrees, minutes and seconds | 2.1 |
Result
Type | Description |
---|---|
number|DMS | Coordinates, coordinate system type WGS-84 |
DMS
Property | Type | Description | API_LEVEL |
---|---|---|---|
direction | string | Direction, E for east longitude, W for west longitude | 2.1 |
degrees | number | degree | 2.1 |
minutes | number | minute | 2.1 |
seconds | number | second | 2.1 |
onChange
Register a callback function to listen for location information change events
onChange(callback: () => void): void
offChange
Cancel the callback function for listening to the location information change event
offChange(callback: () => void): void
Example
import { Geolocation } from '@zos/sensor'
const geolocation = new Geolocation()
const callback = () => {
if (geolocation.getStatus() === 'A') {
console.log(geolocation.getLatitude())
console.log(geolocation.getLongitude())
}
}
geolocation.start()
geolocation.onChange(callback)
// When not needed for use
geolocation.offChange(callback)
geolocation.stop()