Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Geolocation issues and app crashing

I am having two issues while I am trying to capture the device location and use it in my app.

 

1. See code below. It is a simple app to track GPS data, that I will then use to do other stuff. When I test it, the app runs for 30sec to few minutes, showing the location info. Then crashes (goes back to app selection screens).

I don't seem to understand what could be wrong with the code to create the watch to close the app. Any hints?

 

2. If I use watchPosition, as described from the example in the guide, I do not get any position reading at all, nor error messages. Is there something wrong with the guide's code, or am I supposed to add something else?

Thanks

 

import document from "document";
import { geolocation } from "geolocation";
import { display } from "display";
display.autoOff = false;

let gpsStatus = document.getElementById("gps-status");
let gpsLat = document.getElementById("gps-lat");
let gpsLon = document.getElementById("gps-lon");
let gpsHeading = document.getElementById("gps-heading");
let gpsSpeed = document.getElementById("gps-speed");


function refreshData() {

   geolocation.getCurrentPosition(locationSuccess, locationError);

   function locationSuccess(position) {
      console.log("GPS on");
      gpsStatus.innerText = "GPS on" ;
      // gpsLat.innerText = position.coords.latitude;
      // gpsLon.innerText = position.coords.longitude;
      // gpsHeading.innerText = position.coords.heading;
      gpsSpeed.innerText = Math.round(position.coords.speed*3.6) + " KMH"; 
  
    }

    function locationError(error) {
      console.log("Error: " + error.code,
                  "Message: " + error.message);
      // gpsLat.style.fill = "red";
      gpsLat.innerText = "Connecting GPS";
    }

}

refreshData();
setInterval(refreshData, 1000);
Best Answer
0 Votes
15 REPLIES 15

I'm only a journeyman javascript coder, and I haven't had any experience with the Ionic, but.... I see your success and fail functions  located inside the refreshData function, whilst I would have expected to see them outside the refreshData function.

 

Best Answer
0 Votes

If I don't put them inside the refreshData(), how does it refresh my location and calculate speed and distance?

Best Answer
0 Votes

@agsurf5 wrote:

If I don't put them inside the refreshData(), how does it refresh my location and calculate speed and distance?


They are the callback functions for getCurrentPosition().

 

You call refreshData(), that calls getCurrentPosition(). If that is successful, it will call locationSuccess().

 

Best Answer
0 Votes

Hey @JonFitbit I changed it, but it still crashes every time. 30 seconds or so, and it closes.

It is pretty simple, just displaying speed. I am testing to see if I can move ahead in calculating more, but it is really not stable. Any advice?

 

Ps thanks for the continuous help.

 

Here is the code:

import document from "document";
import { geolocation } from "geolocation";

import { display } from "display";
display.autoOff = false;

let gpsStatus = document.getElementById("gps-status");
let gpsLat = document.getElementById("gps-lat");
let gpsLon = document.getElementById("gps-lon");
let gpsHeading = document.getElementById("gps-heading");
let gpsSpeed = document.getElementById("gps-speed");


function refreshData() {

   geolocation.getCurrentPosition(locationSuccess, locationError);

 } 
  

function locationSuccess(position) {
      console.log("GPS on");
      gpsStatus.innerText = "GPS on" ;
      // gpsLat.innerText = position.coords.latitude;
      // gpsLon.innerText = position.coords.longitude;
      // gpsHeading.innerText = position.coords.heading;
      gpsSpeed.innerText = Math.round(position.coords.speed*3.6) + " KMH"; 
  
    }

function locationError(error) {
      console.log("Error: " + error.code,
                  "Message: " + error.message);
      // gpsLat.style.fill = "red";
      gpsStatus.innerText = "Connecting GPS";
      
    }



refreshData();
setInterval(refreshData, 1000);
Best Answer
0 Votes

I would try reducing it to bare minimum, does it still crash?

Just console.log("success") or console.log("failure")

Then slowly add back code to see where it's failing.

 

You could also try coding a bit more defensively.

 

if (position && position.coords && position.coords.speed) {
gpsSpeed.innerText = Math.round(position.coords.speed*3.6) + " KMH";
}

 

Best Answer
0 Votes

I Don't understand, Jon


@JonFitbit wrote:

I would try reducing it to bare minimum, does it still crash?

Just console.log("success") or console.log("failure")

Then slowly add back code to see where it's failing.

 

You could also try coding a bit more defensively.

 

if (position && position.coords && position.coords.speed) {
gpsSpeed.innerText = Math.round(position.coords.speed*3.6) + " KMH";
}

 



. If it's broken, why are you encouraging him to test it further?

Best Answer
0 Votes

@raceQs wrote:

I Don't understand, Jon

. If it's broken, why are you encouraging him to test it further?

watchPosition() isn't working, but it doesn't crash. I think his issue lies elsewhere.

Best Answer
0 Votes

I am not using watchPosition (as it doesn't work), but getCurrentPosition.

I took everything off from screen, I just have the text that show "connecting GPS" and then "GPS on".

No variables, nor numbers moving. It is on for a minute max, then it closes.

 

1. Are you at fitibit using the same functionality for activities? It doesn't seem to crash when using exercises, but I'll make sure to test it again later just in case I have a problem with my own device. Same code/implementation as from the guide?

2. Did anybody successfully implemented  geolocation in an app so far?

 

 

Best Answer

Bumping this up as I am completely stuck in developing my app, if I can't get Geolocation to work.

Did anybody successfully implemented it in an app so far? Mine keep on crashing every minute or so.

Best Answer
0 Votes

After the installation of the last OS update my app with geolocation is working.

 

I'm calling geolocation.getCurrentPosition () every second and normaly I use my app for 30 minutes.

 

Capitano

 

Best Answer
0 Votes

 I'm calling geolocation.getCurrentPosition () every second and normaly I use my app for 30 minutes.

Glad it's working for you. I'd recommend optimizing so you're not excessively draining the battery if you don't need to.

Best Answer
0 Votes

Works for me as well. I will help with optimizing the code, but I am on it as of now. Thanks guys

Best Answer
0 Votes

@JonFitbit wrote:

 I'm calling geolocation.getCurrentPosition () every second and normaly I use my app for 30 minutes.

Glad it's working for you. I'd recommend optimizing so you're not excessively draining the battery if you don't need to.


Thank you for your hint.

 

I want to summarize the distance I run during an exercise. So I guess I have to call the gps-function that way. I could use a longer interval but that reduces the accuracy...

Do you have another suggestion @JonFitbit?

 

Capitano

Best Answer
0 Votes

@Capitano it's just a trade off between the accuracy you need for your app, and the impact that has on battery.

Best Answer
0 Votes

Hi all,

 

do you have problems with geolocation since the last update (OS 27.30.5.16)?

Or do your apps still run without problems?

 

My app resets the ionic since using it.... 😞

 

https://community.fitbit.com/t5/SDK-Development/GPS-failure-back-again/m-p/2315221#M1660

 

Capitano

Best Answer
0 Votes