Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I got strange behavior when I tried to test my "navigator.geolocation.getCurrentPosition" web page. Here is my testing result and code:

my code:

function detectLocation() 
{
  if (navigator.geolocation) 
  {
    navigator.geolocation.getCurrentPosition(geocodePosition, onError, { timeout: 30000 });
    navigator.geolocation.watchPosition(watchGeocodePosition);
  } 
  else 
  {
    onError();
  }
}

this function was run when "body" onload event was called. I had tried to change the timeout to 10000 and 20000, but I still got same result. I also allowed crome and firefox to get my location.

result:

  1. Using chrome (v 17.0.963.79 m), result always went to onError function when navigator.geolocation.getCurrentPosition was called.
  2. Using Firefox (v 10.0.2), result always went to onError function when navigator.geolocation.getCurrentPosition was called.
  3. Using IE (v 9), result was fantastic, I got my current location.

can anyone help me in this strange situation? I really didn't have any idea to solve this problem and I was in hurry on my project deadline. Thanks before.

EDIT : For this couple days I got some progress, the error code code is 2 with a message "Network location provider at 'https://maps.googleapis.com/maps/api/browserlocation/json?browser=chromium&sensor=true' : Response was malformed". Still unsolved, does anyone know how to solve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
555 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...