navigator.geolocation.getCurrentPosition always fail in chrome and firefox

I also got this message:

message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 404.", code: 2

I could solve it by switching on my wifi adapter


I simulated this problem and found that the success callback functions were only called when the html page was hosted on a web server and not when opened from a filesystem.

To test I opened the file directly from my C: drive and it the callbacks didn't work and then hosted the file on Internet Information Services (IIS) and the callbacks did work.

<html>
<body onload="detectLocation()">
<!-- This html must be hosted on a server for navigator.geolocation callbacks to work -->

<div id="status"></div>

<script type="text/javascript">
function detectLocation()
{
  log("detectLocation() starting");
  if (navigator.geolocation)
  {
    log("navigator.geolocation is supported");
    navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
    navigator.geolocation.watchPosition(watchGeocodePosition);
  }
  else
  {
    log("navigator.geolocation not supported");
  }
}
function geocodePosition(){
    log("geocodePosition() starting");
}

function watchGeocodePosition(){
    log("watchGeocodePosition() starting");
}

function onError(error){
    log("error " + error.code);
}
function log(msg){
    document.getElementById("status").innerHTML = new Date() + " :: " + msg + "<br/>" + document.getElementById("status").innerHTML;
}
</script>
</body>
</html>