Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript geolocation on android doesn't work

I am developing a website that relies on pulling the geolocation data of a mobile user. I am doing it the typical way via:

function initialize() {
   if(window.google && google.gears) {
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(useLocation, errorOnLocate);
   }
   else if(navigator.geolocation) {
       navigator.geolocation.getCurrentPosition(useLocation, errorOnLocate);
   }
   else {
       alert("no geo found");
   }
}

function errorOnLocate(message)
{
    alert(message);
    zoomTo(-34.397, 150.644);
}

This always fails with [object PositionError] in to the errorOnLocate function, even if I switch the geolocation method order.

This is on an HTC Hero with android 2.1 using whatever browser is built in. My gps is on and I have instructed the browser to allow websites to view my location. The "location" feature on the google map native application that comes on the phone picks up my location just fine

Further more if I visit my site using FireFox 3.5 on my personal computer it will find my location correctly.(I believe it uses a combination of ip and ap data points). Either way, it uses the same javascript.

EDIT: This is html/js in a browser, not a native app. Also the exact error message is 'The last location provider was disabled'

like image 518
Dan.StackOverflow Avatar asked Jul 30 '10 00:07

Dan.StackOverflow


People also ask

Why is geolocation not accurate?

There is no guarantee the geolocation information will be accurate, because this API can also use the IP address to infer user location in the absence of more accurate technologies, such as GPS receivers on the client's device. The browser exposes this API via the navigator. geolocation property.

Does geolocation work on mobile?

Geolocation uses mobile devices built-in GPS to accurately show where the device, and the user of the device, are located. This data is accessed through apps that the user grants permission to use their location data.

How accurate is HTML5 geolocation?

Depending on the availability of GPS on the device and the quality of the mobile/WIFI signals, HTML5 geolocation can be very accurate i.e. to a street level. Hence it can be used to pin point a device's location making it a very useful technology for websites or apps that require the exact user's location to work.

What is geolocation maximumAge?

maximumAge. A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0 , it means that the device cannot use a cached position and must attempt to retrieve the real current position.


2 Answers

Is this being accessed from a WebView or from the Android Browser app? If from a WebView, you may need to enable geolocation via a Java call. See the WebView reference for that.

Otherwise, I'm not sure precisely what's wrong with your JS, but here's HTML+JS that I know works with the Android browser:

<!DOCTYPE html> 
<html> 
<head> 
  <script type="text/javascript"> 
function watchLocation(successCallback, errorCallback) {
  successCallback = successCallback || function(){};
  errorCallback = errorCallback || function(){};

  // Try HTML5-spec geolocation.
  var geolocation = navigator.geolocation;

  if (geolocation) {
    // We have a real geolocation service.
    try {
      function handleSuccess(position) {
        successCallback(position.coords);
      }

      geolocation.watchPosition(handleSuccess, errorCallback, {
        enableHighAccuracy: true,
        maximumAge: 5000 // 5 sec.
      });
    } catch (err) {
      errorCallback();
    }
  } else {
    errorCallback();
  }
}

function init() {
  watchLocation(function(coords) {
    document.getElementById('test').innerHTML = 'coords: ' + coords.latitude + ',' + coords.longitude;
  }, function() {
    document.getElementById('test').innerHTML = 'error';
  });
}
  </script> 
</head> 

<body onload="init()"> 
  <div id="test">Loading...</div> 
</body> 
</html>
like image 78
Roman Nurik Avatar answered Oct 04 '22 18:10

Roman Nurik


This wasn't working on my Android 2.4, the problem was solved by enabling Wireless Networks Location besides GPS (wasn't working with just GPS)

Settings -> Location & Privacy -> Enable Wireless Networks.

like image 35
Eduardo Chongkan Avatar answered Oct 04 '22 18:10

Eduardo Chongkan