nativescript-community / gps

Tracks GPS location updates regardless of the app state. Does not depend on Google Play Services.
https://nativescript-community.github.io/gps/
Other
10 stars 2 forks source link

GPS for Javascript not TypeScript #7

Open mrwrighty opened 3 years ago

mrwrighty commented 3 years ago

I am migrating a project to NS7+ that had broken plugins that did not support NS7. I've moved from nativescript-geolocation to @nativescript-community/gps

I'm trying to translate the TS example in to JS, I've not made the move to TS as my app is written in JS and annoyed that all examples tend to use TypeScript rather than JavaScript and the documentation is non existent.

Im setting up GPS using the following:

var gps_master = require("@nativescript-community/gps"); var gps = new gps_master();

But get an error

NativeScript encountered a fatal error: Uncaught TypeError: gps_master is not a constructor

How do I declare GPS and get location values in Javascript to pass to ui-mapbox

sivo1981 commented 3 years ago
import * as gps from '@nativescript-community/gps'

this.gps = new gps.GPS()
mrwrighty commented 3 years ago

Thats TypeScript not Javascript isn't it. I need Javascript

farfromrefug commented 3 years ago

it is es6 so it is JavaScript

mrwrighty commented 3 years ago

Getting this error though NativeScript encountered a fatal error: Uncaught TypeError: Cannot set property 'gps' of undefined

Code is as above

import * as gps from '@nativescript-community/gps'

this.gps = new gps.GPS()

farfromrefug commented 3 years ago

@mrwrighty that s normal. this is not defined at top level! you need to integrate that correctly in your app.

mrwrighty commented 3 years ago

Sorry I'm at a complete loss here, I've never used "import" but "require" in all my code as mentioned in my OP. The code is at the top of my page.js file as used previously as as I did with nativescript-geolocation as in:-

var geolocation = require("nativescript-geolocation");

farfromrefug commented 3 years ago

the issue is with this. it is intended to be used in a class object. use a var instead

sivo1981 commented 3 years ago

I have modified your code directly as follows:

var gps_master = require("@nativescript-community/gps");
var gps = new gps_master.GPS();
mrwrighty commented 3 years ago

Many thanks, I'm now getting GPS locations.

Need to integrate for background use irrespective of app state which I understand this plugin will do. Will work on it and come back if I run in to issues. Only considering iOS at the moment.

sivo1981 commented 3 years ago

Many thanks, I'm now getting GPS locations.

Need to integrate for background use irrespective of app state which I understand this plugin will do. Will work on it and come back if I run in to issues. Only considering iOS at the moment.

Background locations for iOS will work as long as the app is not force closed. For Android this will not work out-of-the-box, foreground service needs to be written to get the same background locations when app is minimized.

farfromrefug commented 3 years ago

@mrwrighty indeed as @sivo1981 you need special handling both on iOS and Android:

mrwrighty commented 3 years ago

I previously used nativescript-background-geolocation-lt but support for Nativescript has been withdrawn which is extremely frustrating. There is no reference on the @nativescript-community/gps plugin about background tasks, battery that sort of thing. I'm guessing its a longways off the one I used to use, but I have no options now.

Below is the description of the old plugin I used.

_The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.

The plugin's Philosophy of Operation is to use motion-detection APIs (using accelerometer, gyroscope and magnetometer) to detect when the device is moving and stationary.

When the device is detected to be moving, the plugin will automatically start recording a location according to the configured distanceFilter (meters).

When the device is detected be stationary, the plugin will automatically turn off location-services to conserve energy._