0

I made a simple website with javascript on it that calls to:

navigator.geolocation.getCurrentPosition(show_map, show_map_error);

I have put the website on the internet. I tried to open the website from different PCs (no GPS gadget) on different locations. One from my home, one from a friends office.

But the script does not always get a position. What would be a problem?

Thank you.

fasisi
  • 396
  • 5
  • 23

1 Answers1

0

The method is not guaranteed to return a position, especially if there is no GPS attached.

You could try getting a cached position instead. See the following from the API specification

// Request a position. We only accept cached positions, no matter what 
// their age is. If the user agent does not have a cached position at
// all, it will immediately invoke the error callback.
navigator.geolocation.getCurrentPosition(successCallback,
                                         errorCallback,
                                         {maximumAge:Infinity, timeout:0});

function successCallback(position) {
  // By setting the 'maximumAge' to Infinity, the position
  // object is guaranteed to be a cached one.
  // By using a 'timeout' of 0 milliseconds, if there is
  // no cached position available at all, the user agent 
  // will immediately invoke the error callback with code
  // TIMEOUT and will not initiate a new position
  // acquisition process.
  if (position.timestamp < freshness_threshold && 
      position.coords.accuracy < accuracy_threshold) {
    // The position is relatively fresh and accurate.
  } else {
    // The position is quite old and/or inaccurate.
  }
}

function errorCallback(error) {
  switch(error.code) {
    case error.TIMEOUT:
      // Quick fallback when no cached position exists at all.
      doFallback();
      // Acquire a new position object.
      navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
      break;
    case ... // treat the other error cases.
  };
}

function doFallback() {
  // No cached position available at all.
  // Fallback to a default position.
}
Matt Tew
  • 1,581
  • 1
  • 9
  • 15
  • If a PC has no GPS attached, why I can get a position? – fasisi Aug 29 '12 at 02:41
  • And I have a blackberry phone with integrated GPS. The phone's map application can get position from its GPS. But the website can't get a position. What is the problem? – fasisi Aug 29 '12 at 02:43
  • According to the specification, a device can use a number of methods to approximate position, including IP address. – Matt Tew Aug 29 '12 at 03:17
  • I am not sure about the blackberry, but perhaps the browser is not set up to use the GPS position, or you have not given it permission to do so. – Matt Tew Aug 29 '12 at 03:18
  • I found on a website that blackberry has different way to access its GPS coordinate. I'll post the URL later. – fasisi Aug 30 '12 at 02:13