4

I'm trying to use Geolocation with the HTML5 API on Android devices. I did my tests with a Galaxy S LC (equivalent to Galaxy S1). The Android version is 2.2.1.

I have read many topics about it but I still didn't find a solution.

I use this typical code for Geolocating :

`

<script type="text/javascript">

document.write("Trying to locate you");

if(navigator.geolocation){
    document.write("Geolocation is supported");

    navigator.geolocation.getCurrentPosition(successCallback, errorCallback,
  {
    enableHighAccuracy : true,
    timeout : 10000, // 10s
    maximumAge : 0
  }
);


function successCallback(position){
    document.write("You have been located. Latitude :" + position.coords.latitude +", longitude : " + position.coords.longitude);       
};


function errorCallback(error){
    document.write("It didn't work");
    switch(error.code){
    case error.PERMISSION_DENIED:
        document.write("Permission denied");
        break;
    case error.POSITION_UNAVAILABLE:
        document.write("Position unavailable");
        break;
    case error.TIMEOUT:
        document.write("Timeout");
        break;
    case error.UNKNOW_ERROR:
        document.write("Unknown error");
        break;              
    }
}

}
else{
    document.write("La géolocalisation n'est pas supportée par le navigateur"); 
}

</script>

It works well on PC with Chrome and Opera (but not with Firefox). It also works with Opera on the mobile device. But it doesn't work with the Android browser. Its version is 2.2.1. This is what's happening on this one : 1. The following messages appear: "Trying to locate you" "Geolocation is supported" 2. After the time specified by the timeout (10s here) a blank page is displayed. The functions sucessCallback or errorCallback are not called.

When I don't specify a timeout (timeout = infinity), it works if GPS is enabled. If it's not, I also get a blank page without error messages.

I tried this HTML5 Geolocation test on the Android browser : http://html5demos.com/geo and it also doesn't work.

I tried this code (1st answer). It works sometimes but we have to wait 2 min to get the position (as said in the comments).

I also tried to get it trhough a Webview with all the specifications needed but it does exactly the same.

Here it is said that to resolve this bug on Android browser we just need to specify the enableHighAccuracy to true, but if the GPS isn't activated there is still no function (sucessCallback or errorCallback) called.

If someone has a solution it would be a great help for me.

Thank you for your help.


Actually the blank page was caused by the "document.write" function. I replaced it by innerHTML and it now works. That means that errors are displayed, the errorCallback is called.

Now the problem is that the Android browser can only determine the position using the GPS (HighAccuracy). If GPS isn't enabled or if it's not able to pick up the position (inside a building for example), it won't return a position. On the contrary the Opera browser will use the relay network (Cell-ID method) to determine the position (in that case accuracy is less than 150m).

Do you know how to force the Android browser to use the cell-ID source ?

Community
  • 1
  • 1
  • Check this post and its solutions. http://stackoverflow.com/questions/11280946/unable-to-get-gps-coordinates-using-javascript-on-browser-in-android/11298468#11298468 – axs Jul 02 '12 at 19:52

2 Answers2

0

Try it like this. That should do it.

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition,showError,
          {
            enableHighAccuracy : true,
            timeout : 10000, // 10s
            //maximumAge : 0
          }
        );
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }
function showError(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
      x.innerHTML="User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML="Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML="The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML="An unknown error occurred."
      break;
    }
  }
</script>

</body>
</html>
0

Google changed the behavior of location services in Chrome this January. One of the changes was that Chrome does not return current location coords if http connection is not encrypted. We had similar issue few months ago where location services in our web app would work fine on all clients on all devices (PCs, phones, tablets, etc) except for Chrome browser. Installing SSL certificate on that app fixed all geo location issues. Hope this helps.

Alex
  • 566
  • 1
  • 6
  • 14