Location
API Reference: Device.Location
Location allows capturing location-related events on the device

Android Location Provider

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.
TypeScript
1
import PageSampleDesign from 'generated/pages/pageSample';
2
import FlexLayout from '@smartface/native/ui/flexlayout';
3
import Application from '@smartface/native/application';
4
import Location from '@smartface/native/device/location';
5
import PermissionUtil from '@smartface/extension-utils/lib/permission';
6
import System from '@smartface/native/device/system';
7
8
//You should create new Page from UI-Editor and extend with it.
9
export default class Sample extends PageSampleDesign {
10
11
constructor() {
12
super();
13
// Overrides super.onShow method
14
this.onShow = onShow.bind(this, this.onShow.bind(this));
15
// Overrides super.onLoad method
16
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
17
18
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
19
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
20
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
21
}
22
23
getCurrentLocation(): void {
24
Location.start(Location.Android.Priority.HIGH_ACCURACY, 1000);
25
Location.onLocationChanged = (e): void => {
26
console.log(`Location latitude ${e.latitude} longitude ${e.longitude}`);
27
Location.stop();
28
};
29
}
30
}
31
32
/**
33
* @event onShow
34
* This event is called when a page appears on the screen (everytime).
35
* @param {function} superOnShow super onShow function
36
* @param {Object} parameters passed from Router.go function
37
*/
38
function onShow(superOnShow: () => void) {
39
const { headerBar } = this;
40
superOnShow();
41
Application.statusBar.visible = false;
42
headerBar.visible = false;
43
}
44
45
/**
46
* @event onLoad
47
* This event is called once when page is created.
48
* @param {function} superOnLoad super onLoad function
49
*/
50
function onLoad(superOnLoad: () => void) {
51
superOnLoad();
52
53
// Location permission is guaranteed.
54
55
// Shows the request dialog if needed
56
//@ts-ignore
57
Location.android.checkSettings({
58
onSuccess: () => {
59
console.log("Location.checkSettings onSuccess");
60
PermissionUtil.getPermission(Application.Android.Permissions.ACCESS_FINE_LOCATION, 'Please go to the settings and grant permission')
61
.then(() => {
62
this.getCurrentLocation();
63
})
64
.then((reason) => {
65
console.info('Permission rejected');
66
});
67
},
68
onFailure: (statusCode: Location.Android.SettingsStatusCodes) => {
69
console.log("Location.checkSettings onFailure");
70
if (statusCode == Location.Android.SettingsStatusCodes.DENIED) {
71
console.log("SettingsStatusCodes.DENIED");
72
}
73
else {
74
// go to settings via Application.call
75
console.log("SettingsStatusCodes.OTHER" + Location.Android.SettingsStatusCodes.OTHER);
76
}
77
}
78
});
79
80
if(System.OS === 'iOS') {
81
this.getCurrentLocation();
82
}
83
}
84
Copied!

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.
Untitled
1
function onHide(superOnHide) {
2
superOnHide && superOnHide();
3
Location.stop();
4
}
Copied!

Location Optimization On Android

In order to prevent redundant power consumption, location architecture on Android has been optimized. Before running Location.start, Android permissions must be checked.
Last modified 2mo ago
Copy link