Location
API Reference: Device.Location
Location allows capturing location-related events on the device
Android automatically picks a location provider unless you provide manually. This may lead undesired consequences; for instance, GPS provider might be selected in an indoor environment which will cause location update problems.
import PageSampleDesign from "generated/pages/page3";
import { Route, Router } from "@smartface/router";
import Location from "@smartface/native/device/location";
import Permission from "@smartface/native/device/permission";
import System from "@smartface/native/device/system";
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
constructor(private router?: Router, private route?: Route) {
super({});
}
getCurrentLocation(): void {
Location.start("HIGH_ACCURACY", 1000);
Location.onLocationChanged = (e): void => {
console.log(`Location latitude ${e.latitude} longitude ${e.longitude}`);
Location.stop();
};
}
onShow() {
super.onShow();
const { headerBar } = this;
Application.statusBar.visible = false;
headerBar.visible = false;
}
onLoad() {
super.onLoad();
if (System.OS === System.OSType.IOS) {
this.getCurrentLocation();
} else {
Location.android.checkSettings({
onSuccess: () => {
console.log("Location.checkSettings onSuccess");
Permission.android
.requestPermissions(Permissions.location.approximate)
.then((result) => {
if (result[0] === PermissionResult.GRANTED) {
this.getCurrentLocation();
}
});
},
onFailure: ({ statusCode }) => {
console.log("Location.checkSettings onFailure");
if (statusCode == Location.Android.SettingsStatusCodes.DENIED) {
console.log("SettingsStatusCodes.DENIED");
} else {
// go to settings via Application.call
console.log(
"SettingsStatusCodes.OTHER" +
Location.Android.SettingsStatusCodes.OTHER
);
}
},
});
}
}
}
Stop Location Updates
Consider the stop location updates whether your location no longer in use or your page is does not appear on screen. This can be handy to reduce power consumption, provided the app doesn't need to collect information even when it's running in the background. This section shows how you can stop location updates in page onHide.
function onHide(superOnHide) {
superOnHide && superOnHide();
Location.stop();
}
Location Permissions in Android
There are multiple permissions related to location in Android.
- approximate -> used for approximate location access
- precise -> used for precise location access
To better respect user privacy, it's recommended that you only request approximate. You can fulfill most use cases even when you have access to only approximate location information.
Approximate Location
You can use approximate permisison for approximate location access.
Permission.android
.requestPermissions(Permissions.location.approximate)
.then((result) => {
if (result[0] === PermissionResult.GRANTED) {
// get location
}
});
Precise Location
You can use precise permission for precise location access.
Permission.android
.requestPermissions(Permissions.location.precise)
.then((result) => {
if (result[0] === PermissionResult.GRANTED) {
// get location
}
});
If user has given precise permission it will enable both precise permission and approximate permission. If the user has given approximate location permission and precise location access is required, you can request an upgrade to precise location permission.
To request an upgrade to precise location permission, just request precise permission.
In order to prevent redundant power consumption, location architecture on Android has been optimized. Before running Location.start, Android permissions must be checked.
Some native features might require specific permissions and published app to work on Smartface due to Smartface Android Emulator itself not having necessary permissions.
List of permissions disabled by the emulator:
- android.permission.ACCESS_BACKGROUND_LOCATION
You can add this permission to your AndroidManifest.xml file to enable the feature on your published app.