0

I'm using HTML Geolocation API to get the location of the user in web application after some interval. When the web application is in the foreground it is working fine on mobile browsers even in safari on iPhone and also in google chrome on android. But when the web application in the browser goes in the background or when mobile goes into the sleep mode then I am unable to get the location using HTML Geolocation API. Below is the sample code

<script>

setInterval(function(){ 
    getLocation();
}, 10000);

var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
 // save the position
}
</script>   

Any help?

Zohaib Akram
  • 19
  • 1
  • 6

1 Answers1

0

Firstly you should probably be using watchPosition() rather then getCurrent at an interval.

Secondly geolocation does not work in the background :-(

This requirement is well known and has been for over 5 years. See lengthy discussion here

A POC design and solution has also been available for years

To date NO ONE can fault the solution! Yet W3C refuses to spec it and browsers refuse to implement it :-( The reasons why are known only unto them.

McMurphy
  • 1,235
  • 1
  • 15
  • 39