Location Kit provides location services using multiple positioning technologies, such as Global Navigation Satellite System (GNSS) positioning, cellular positioning, and WLAN/Bluetooth positioning. Cellular positioning and WLAN/Bluetooth positioning are collectively referred to as network positioning technologies hereinafter. With these positioning technologies, the location of a user device can be accurately determined whether the device is indoors or outdoors.
A coordinate describes a location on the earth using the longitude and latitude in reference to the World Geodetic Coordinate System 1984.
GNSS positioning determines the location of a mobile device by using the positioning algorithms offered by the device chip to process location data from GNSS, for example, GPS, GLONASS, BeiDou, and Galileo. The specific positioning system used during the process depends on the hardware capabilities of the device.
Cellular positioning estimates the current location of a mobile device based on the location of the serving base station and neighboring base stations. This technology offers relatively low accuracy and requires access to the cellular network.
WLAN or Bluetooth positioning estimates the current location of a mobile device based on the locations of WLAN access points (APs) and Bluetooth devices that can be discovered by the device. The location accuracy of this technology depends on the distribution of fixed WLAN APs and Bluetooth devices around the device. A higher density of WLAN APs and Bluetooth devices can produce a more accurate location result than cellular positioning. This technology also requires access to the network.
Location awareness is offered by the system as a basic service for atomic services. Depending on the service scenario, an atomic service needs to initiate a location request to the system and stop the location request when the service scenario ends. In this process, the system reports the location result to the atomic service on a real-time basis.
Your atomic service can use the location function only after the user has granted the permission and turned on the function. If the location function is off, the system will not provide the location service for any atomic service.
Since the location information is considered sensitive, your atomic service still needs to obtain the location access permission from the user even if the user has turned on the location function. The system will provide the location service for your atomic service only after it has been granted the permission to access the device location information.
Before using location capabilities provided by the system, check whether your atomic service has been granted the permission to access the device location information. If not, your atomic service needs to obtain the permission from the user.
The system provides the following location permissions:
ohos.permission.LOCATION: used to obtain location accurate to meters.
ohos.permission.APPROXIMATELY_LOCATION: used to obtain location accurate to 5 kilometers.
If your atomic service needs to access the device location information, it must first request required permissions.
You can declare the required permissions in the atomic service configuration file. For details, please refer to Authorization.
has.authorize({
scope: 'scope.userLocation',
success: (res) => {
has.getLocation({
type: 'wgs84',
success: (res) => {
const latitude = res.latitude;
const longitude = res.longitude;
const speed = res.speed;
const accuracy = res.accuracy;
console.info('getLocation success', res);
},
fail: (err) => {
console.error('getLocation fail', err);
},
complete: (res) => {
console.info('getLocation complete', res);
}
});
console.info('authorize success', res);
},
fail: (err) => {
console.error('authorize fail', err);
},
complete: (res) => {
console.info('authorize complete', res);
}
});