5.1.0 • Published 5 years ago

nativescript-geolocation v5.1.0

Weekly downloads
1,447
License
Apache-2.0
Repository
github
Last release
5 years ago

NativeScript Geolocation apple android

npm npm Build Status

Geolocation plugin to use for getting current location, monitor movement, etc.

Installation

In Command prompt / Terminal navigate to your application root folder and run:

tns plugin add nativescript-geolocation

Usage

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:

Import the plugin

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");

Request permissions

geolocation.enableLocationRequest();

Call plugin methods

// Get current location with high accuracy
geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 5000, timeout: 20000 })

API

Properties

Location

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

Options

PropertyDefaultDescription
desiredAccuracy?Accuracy.highThis 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.
updateDistanceNo filterUpdate distance filter in meters. Specifies how often to update the location. Read more in Apple document and/or Google documents
updateTime1 minuteInterval between location updates, in milliseconds (ignored on iOS). Read more in Google document.
minimumUpdateTime5 secsMinimum time interval between location updates, in milliseconds (ignored on iOS). Read more in Google document.
maximumAge-How old locations to receive in ms.
timeout5 minutesHow long to wait for a location in ms.
iosAllowsBackgroundLocationUpdatesfalseIf 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
iosPausesLocationUpdatesAutomaticallytrueAllow deactivation of the automatic pause of location updates (ignored on Android). Read more in Apple document
iosOpenSettingsIfLocationHasBeenDeniedfalseArgument on the enableLocationRequest. If true, the settings app will open on iOS so the user can change the location services permission.

If iosAllowsBackgroundLocationUpdates is set to true, the following code is required in the info.plist file:

<key>UIBackgroundModes</key>
<array>
   <string>location</string>
</array>

Methods

MethodReturnsDescription
getCurrentLocation(options: Options)PromiseGet 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)numberMonitor for location change.
clearWatch(watchId: number)voidStop monitoring for location change. Parameter expected is the watchId returned from watchLocation.
enableLocationRequest(always?: boolean)Promise\<void>Ask for permissions to use location services. The option always is applicable only for iOS. For a custom prompt message on IOS, the following keys are required. NSLocationAlwaysUsageDescription, NSLocationUsageDescription and NSLocationWhenInUseUsageDescription Read more about its usage .
isEnabledPromise\<boolean>Resolves true or false based on the location services availability.
distance(loc1: Location, loc2: Location)numberCalculate the distance between two locations. Returns the distance in meters.

Known Issues

Version Conflicts – Google Play Services

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.+"
  }
}

Contribute

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.

Get Help

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.

npm.io

5.1.0

5 years ago

5.0.0

5 years ago

4.5.0

5 years ago

4.4.2

5 years ago

4.4.1

5 years ago

4.4.0

5 years ago

4.3.1

6 years ago

4.3.0

6 years ago

4.2.6

6 years ago

4.2.5

6 years ago

4.2.4

6 years ago

4.2.3

6 years ago

4.2.2

6 years ago

4.2.1

6 years ago

4.2.0

7 years ago

4.1.1

7 years ago

4.1.0

7 years ago

4.0.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago