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.

JavaScript
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const Location = require('sf-core/device/location');
const Application = require('sf-core/application');
module.exports = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
// Location permission is guaranteed.
// Shows the request dialog if needed
Location.android.checkSettings({
onSuccess: function() {
console.log("Location.checkSettings onSuccess");
const requestCode = 101; // Anything can be given
if (Application.android.checkPermission(Application.android.Permissions.ACCESS_FINE_LOCATION)) {
getCurrentLocation();
}
else {
Application.android.requestPermissions(requestCode, Application.android.Permissions.ACCESS_FINE_LOCATION);
}
Application.android.onRequestPermissionsResult = function(e) {
if (e.requestCode == requestCode) {
getCurrentLocation();
}
};
},
onFailure: function(e) {
console.log("Location.checkSettings onFailure");
if (e.statusCode == Location.Android.SettingsStatusCodes.DENIED) {
console.log("SettingsStatusCodes.DENIED");
}
else {
// go to settings via Application.call
console.log("SettingsStatusCodes.OTHER" + Location.Android.SettingsStatusCodes.OTHER);
}
}
});
function getCurrentLocation() {
Location.start(Location.Android.Priority.HIGH_ACCURACY,1000);
Location.onLocationChanged = function(e) {
console.log(`Location latitude ${e.latitude} longitude ${e.longitude}`);
Location.stop();
};
}
}
);

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.

JavaScript
JavaScript
function onHide(superOnHide) {
superOnHide && superOnHide();
Location.stop();
}

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.