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

geolocation app fails after 2 minutes and ionic restart

ANSWERED

I am writing a app with using the geolocation interface. I can build and run the app without errors. But after 2-3 minutes run this app my Ionic fails and restart. Anyone have an idea what is wrong in my script?

-------------------------------------------------------------------

/* geolocation_example_display_output */
import { geolocation } from "geolocation";
import document from "document";
 
/* variables display */
let latitudeData          = document.getElementById("latitude-data");
let longitudeData         = document.getElementById("longitude-data");
let positionAccuracyData  = document.getElementById("positionAccuracy-data");
let headingData           = document.getElementById("heading-data");
let speedData             = document.getElementById("speed-data");
let altitudeData          = document.getElementById("altitude-data");
let altitudeAccuracyData  = document.getElementById("altitudeAccuracy-data");
let timestampData         = document.getElementById("timestamp-data");
let errorData             = document.getElementById("error-data");
let messageData           = document.getElementById("message-data");
 
/* options gps */
var locationOptions = {
  enableHighAccuracy: true,
  timeout: 10000,
  maximumAge: 0
};
 

function getPosition() {
  geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
  console.log("start loop getCurrentPosition");
}
function locationSuccess(position) {
  console.log("Latitude: "            + position.coords.latitude,
              "Longitude: "           + position.coords.longitude,
              "Position Accuracy : "  + position.coords.accuracy,
              "Heading: "             + position.coords.heading,
              "Speed: "               + position.coords.speed,
              "Altitude: "            + position.coords.altitude,
              "Altitude Accuracy: "   + position.coords.altitudeAccuracy,
              "Time Stamp: "          + position.timestamp);

              latitudeData.text         = (position.coords.latitude);
              longitudeData.text        = (position.coords.longitude);
              positionAccuracyData.text = (position.coords.accuracy);
              headingData.text          = (position.coords.heading);
              speedData.text            = (position.coords.speed);
              altitudeData.text         = (position.coords.altitude);
              altitudeAccuracyData.text = (position.coords.altitudeAccuracy);
              timestampData.text        = (position.timestamp);
}

function locationError(error) {
  console.log("Error: "     + error.code,
              "Message: "   + error.message);
  messageData.text =         (error.message);
}

setInterval(getPosition, 5000);
Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

You're probably flooding requests to getPosition() because you aren't waiting for success.

 

You should probably either switch over to watchPosition(), or stop using setInterval(), and use setTimeout() after location success. Something like:

 

function getPosition() {
  geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
  console.log("start loop getCurrentPosition");
}

function locationSuccess(position) {
  // ...
  setTimeout(getPosition, 5000);
}

// Start getting position
getPosition();

View best answer in original post

Best Answer
0 Votes
1 REPLY 1

You're probably flooding requests to getPosition() because you aren't waiting for success.

 

You should probably either switch over to watchPosition(), or stop using setInterval(), and use setTimeout() after location success. Something like:

 

function getPosition() {
  geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
  console.log("start loop getCurrentPosition");
}

function locationSuccess(position) {
  // ...
  setTimeout(getPosition, 5000);
}

// Start getting position
getPosition();
Best Answer
0 Votes