Skip to main content
Version: v2

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

PropertyTypeRequiredDefaultValueDescriptionAPI_LEVEL
formatstringNDMSCoordinate format, optionally DD for decimal or DMS in degrees, minutes and seconds2.1

Result

TypeDescription
number|DMSCoordinates, coordinate system type WGS-84

DMS

PropertyTypeDescriptionAPI_LEVEL
directionstringDirection, N for north latitude, S for south latitude2.1
degreesnumberdegree2.1
minutesnumberminute2.1
secondsnumbersecond2.1

getLongitude

Get Longitude

getLongitude(option: Option): Result

Option

PropertyTypeRequiredDefaultValueDescriptionAPI_LEVEL
formatstringNDMSCoordinate format, optionally DD for decimal or DMS in degrees, minutes and seconds2.1

Result

TypeDescription
number|DMSCoordinates, coordinate system type WGS-84

DMS

PropertyTypeDescriptionAPI_LEVEL
directionstringDirection, E for east longitude, W for west longitude2.1
degreesnumberdegree2.1
minutesnumberminute2.1
secondsnumbersecond2.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()