Geolocation plugin to use for getting current location, monitor movement, etc.
In Command prompt / Terminal navigate to your application root folder and run:
tns plugin add nativescript-geolocation
The best way to explore the usage of the plugin is to inspect the demo app in the plugin's root folder.In demo
folder you can find the usage of the plugin for TypeScript non-Angular application. Refer to demo/app/main-page.ts
.
In short here are the steps:
TypeScript
import * as geolocation from "nativescript-geolocation";
import { Accuracy } from "tns-core-modules/ui/enums"; // used to describe at what accuracy the location should be get
Javascript
var geolocation = require("nativescript-geolocation");
geolocation.enableLocationRequest();
// Get current location with high accuracy
geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 5000, timeout: 20000 })
Property | Default | Description |
---|---|---|
latitude | - | The latitude of the geolocation, in degrees. |
longitude | - | The longitude of the geolocation, in degrees. |
altitude | - | The altitude (if available), in meters above sea level. |
horizontalAccuracy | - | The horizontal accuracy, in meters. |
verticalAccuracy | - | The vertical accuracy, in meters. |
speed | - | The speed, in meters/second over ground. |
timestamp | - | The time at which this location was determined. |
Property | Default | Description |
---|---|---|
desiredAccuracy? | Accuracy.high | This will return the finest location available but use more power than any option. Accuracy.any is considered to be about 100 meter accuracy. Using a coarse accuracy such as this often consumes less power. |
updateDistance | No filter | Update distance filter in meters. Specifies how often to update the location. Read more in Apple document and/or Google documents |
updateTime | 1 minute | Interval between location updates, in milliseconds (ignored on iOS). Read more in Google document. |
minimumUpdateTime | 5 secs | Minimum time interval between location updates, in milliseconds (ignored on iOS). Read more in Google document. |
maximumAge | - | How old locations to receive in ms. |
timeout | 5 minutes | How long to wait for a location in ms. |
iosAllowsBackgroundLocationUpdates | false | If enabled, UIBackgroundModes key in info.plist is required (check the hint below). Allow the application to receive location updates in background (ignored on Android). Read more in Apple document |
iosPausesLocationUpdatesAutomatically | true | Allow deactivation of the automatic pause of location updates (ignored on Android). Read more in Apple document |
If iosAllowsBackgroundLocationUpdates is set to true, the following code is required in the info.plist file:
<key>UIBackgroundModes</key> <array> <string>location</string> </array>
Method | Returns | Description |
---|---|---|
getCurrentLocation(options: Options) | Promise | Get current location applying the specified options (if any). Since version 5.0 of the plugin, it will use requestLocation API for devices using iOS 9.0+. In situation of poor or no GPS signal, but available Wi-Fi it will take 10 sec to return location. |
watchLocation(successCallback: successCallbackType, errorCallback: errorCallbackType, options: Options) | number | Monitor for location change. |
clearWatch(watchId: number) | void | Stop monitoring for location change. Parameter expected is the watchId returned from watchLocation . |
enableLocationRequest(always?: boolean, iosOpenSettingsIfLocationHasBeenDenied?: boolean) | Promise<void> | Ask for permissions to use location services. The options are applicable only for iOS. When always is true, it opens a custom prompt message and the following keys are required: NSLocationAlwaysAndWhenInUseUsageDescription (iOS 11.0+) OR NSLocationAlwaysUsageDescription (iOS 8.0-10.0) and NSLocationWhenInUseUsageDescription. Read more about request always usage . When iosOpenSettingsIfLocationHasBeenDenied is true, the settings app will open on iOS so the user can change the location services permission. |
isEnabled | Promise<boolean> | Resolves true or false based on the location services availability. |
distance(loc1: Location, loc2: Location) | number | Calculate the distance between two locations. Returns the distance in meters. |
If you have installed multiple plugins that use the Google Play Services you might run into version conflicts. For example, you may encounter the error below when using the nativescript-google-maps-sdk plugin:
Cannot enable the location service. Error: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/internal/zzbck;
In order to fix this you might pin the version number in your app/App_Resources/Android/before-plugins.gradle
file (if the file does not exist, just create it):
android {
// other stuff here
project.ext {
googlePlayServicesVersion = "16.+"
}
}
We love PRs! Check out the contributing guidelines. If you want to contribute, but you are not sure where to start - look for issues labeled help wanted
.
Please, use github issues strictly for reporting bugs or requesting features. For general questions and support, check out Stack Overflow or ask our experts in NativeScript community Slack channel.
安装 geolocation 插件 执行以下命令 npm install --save @ionic-native/geolocation 将geolocation插件在app.module.ts内声明 import { Geolocation } from '@ionic-native/geolocation'; @NgModule({ providers: [ { provide:
NativeScript 可以使用 Javascript,CSS, XML 创建真正的 Native 跨平台应用,支持 iOS Android,NativeScript 将您的跨平台代码翻译成目标平台的代码。 UI 使用 XML 描述,CSS 样式,在编译时将 UI 转化成本地原生代码,最终得到正在的 Native 原生应用。 Telerik 公开了用于创建安卓、iOS和Windows Unive
NativeScript Command-Line Interface The NativeScript CLI lets you create, build, and deploy NativeScript-based apps on iOS and Android devices. Get it using: npm install -g nativescript What is Native
NativeScript-Snackbar �� �� �� NativeScript plugin for Material Design SnackBar component. Installation: NativeScript 7+:tns plugin add @nstudio/nativescript-snackbar NativeScript version prior to 7:t
Nativescript-Ripple This plugin aims to bring a native (or close to native) ripple implementation on Android and iOS. The android version uses a RippleDrawable and conserves the previous background, a
NativeScript-FloatingActionButton NativeScript plugin for Material Design Floating Action Button UI component. Installation Nativescript 7+: ns plugin add @nstudio/nativescript-floatingactionbutton Na
NativeScript CardView A NativeScript plugin to provide an XML widget to implement the Material Design CardView component. Installation NativeScript 7+: ns plugin add @nstudio/nativescript-cardview Nat