Skip to main content
Version: 7.0.0

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
import PageSampleDesign from "generated/pages/page3";
import { Route, Router } from "@smartface/router";
import Application from "@smartface/native/application";
import Location from "@smartface/native/device/location";
import PermissionUtil from "@smartface/extension-utils/lib/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(Location.Android.Priority.HIGH_ACCURACY, 1000);
Location.onLocationChanged = (e): void => {
console.log(`Location latitude ${e.latitude} longitude ${e.longitude}`);
Location.stop();
};
}

// The page design has been made from the code for better
// showcase purposes. As a best practice, remove this and
// use WYSIWYG editor to style your pages.
centerizeTheChildrenLayout() {
this.dispatch({
type: "updateUserStyle",
userStyle: {
flexProps: {
flexDirection: 'ROW',
justifyContent: 'CENTER',
alignItems: 'CENTER'
}
}
})
}

onShow() {
super.onShow();
const { headerBar } = this;
Application.statusBar.visible = false;
headerBar.visible = false;

}

onLoad() {
super.onLoad();
this.centerizeTheChildrenLayout();

Location.android.checkSettings({
onSuccess: () => {
console.log("Location.checkSettings onSuccess");
PermissionUtil.getPermission(
{
androidPermission: Application.Android.Permissions.ACCESS_FINE_LOCATION,
permissionText: "Please go to the settings and grant permission"
})
.then(() => {
this.getCurrentLocation();
})
.then((reason) => {
console.info("Permission rejected");
});
},
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
);
}
},
});

if (System.OS === System.OSType.IOS) {
this.getCurrentLocation();
}
}
}

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 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.