API Reference: Device.Location​
Location allows capturing location-related events on the device
TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages
. You can then extend that class with the following TypeScript classes.
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/pageSample';import FlexLayout = require('sf-core/ui/flexlayout');import Application = require('sf-core/application');import Location = require('sf-core/device/location');import PermissionUtil = require('sf-extension-utils/lib/permission');import System = require('sf-core/device/system');​//You should create new Page from UI-Editor and extend with it.export default class Sample extends PageSampleDesign {​constructor() {super();// Overrides super.onShow methodthis.onShow = onShow.bind(this, this.onShow.bind(this));// Overrides super.onLoad methodthis.onLoad = onLoad.bind(this, this.onLoad.bind(this));​this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;this.layout.alignItems = FlexLayout.AlignItems.CENTER;}​getCurrentLocation(): void {Location.start(Location.Android.Priority.HIGH_ACCURACY, 1000);Location.onLocationChanged = (e): void => {console.log(`Location latitude ${e.latitude} longitude ${e.longitude}`);Location.stop();};}}​/*** @event onShow* This event is called when a page appears on the screen (everytime).* @param {function} superOnShow super onShow function* @param {Object} parameters passed from Router.go function*/function onShow(superOnShow: () => void) {const { headerBar } = this;superOnShow();Application.statusBar.visible = false;headerBar.visible = false;}​/*** @event onLoad* This event is called once when page is created.* @param {function} superOnLoad super onLoad function*/function onLoad(superOnLoad: () => void) {superOnLoad();​// Location permission is guaranteed.​// Shows the request dialog if needed//@ts-ignoreLocation.android.checkSettings({onSuccess: () => {console.log("Location.checkSettings onSuccess");PermissionUtil.getPermission(Application.Android.Permissions.ACCESS_FINE_LOCATION, 'Please go to the settings and grant permission').then(() => {this.getCurrentLocation();}).then((reason) => {console.info('Permission rejected');});},onFailure: (statusCode: Location.Android.SettingsStatusCodes) => {console.log("Location.checkSettings onFailure");if (statusCode == Location.Android.SettingsStatusCodes.DENIED) {console.log("SettingsStatusCodes.DENIED");}else {// go to settings via Application.callconsole.log("SettingsStatusCodes.OTHER" + Location.Android.SettingsStatusCodes.OTHER);}}});​if(System.OS === 'iOS') {this.getCurrentLocation();}}​
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 neededLocation.android.checkSettings({onSuccess: function() {console.log("Location.checkSettings onSuccess");const requestCode = 101; // Anything can be givenif (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.callconsole.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();};}​});
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();}
In order to prevent redundant power consumption, location architecture on Android has been optimized. Before running Location.start, Android permissions must be checked.
​