Search code examples
javascriptphpandroidgeolocationgps

Get coordinates from Hardware GPS


I've found a lot of questions about GPS Coordinates but not one that confirms using the mobile hardware GPS instead of Web GPS like geoLocation and such like.

My actual method:

I'm using navigator.geolocation.getCurrentPosition(), the Lat/Long comes from the Web, here's the code:

function getGPS(funcCallBack)
{
   if (navigator.geolocation)
   {
     var timeoutVal = getCookie("GPSTimeout");
     navigator.geolocation.getCurrentPosition(sucess
                                             ,error
                                             ,{enableHighAccuracy: true
                                              ,timeout: timeoutVal
                                              ,maximumAge: 0}
                                             );
   }
   else{
   alert('GPS is turned off, or was not possible to find it. Now, doing the login without localization.');
   window.gpsLat = 0;
   window.gpsLng = 0;
   window.gpsAcc = 0;
   funcCallBack();
   }
   function sucess(position) //sucess
   {
      window.gpsLat = position.coords.latitude;
      window.gpsLng = position.coords.longitude;
      window.gpsAcc = position.coords.accuracy;
      funcCallBack();
    }
    function error()         //error
    {
      window.gpsLat   = 0;
      window.gpsLng   = 0;
      window.gpsAcc   = 0;
      funcCallBack();
    }
}

My problem:

Sometimes when I do the login I am not getting the GPS Coordinates (they come 0) and sometimes I am getting coordinates with more than 2,000 Accuracy (that is not precise).

By the way, I am testing the GPS on a data internet service, when I do use a Wi-Fi connection it works perfectly with less than 100 Accuracy.

Details:

Maybe you are complaining about:

  1. timeoutVal: it is a cookie with the number 5000 inside it.
  2. funcCallBack: it is a function that continues the login operation.
  3. window.gpsLat: it is a global var containing the Latitude value got from the geoLocation.
  4. window.gpsLng: it is a global var containing the Longitude value got from the geoLocation.
  5. window.gpsAcc: it is a global var containing the Accuracy value got from the geoLocation.

What do I want?

I want a solution in JavaScript or PHP that can get coordinates from the mobile hardware device, the Native GPS, not the geolocation, and when the Native GPS is turned off, ask the user to turn it on.


Solution

  • You should get the location with javascript not PHP. PHP is only capable of doing an IP lookup which is the least accurate method for determining location.

    The way navigator.geolocation.getCurrentPosition() works is it uses the most accurate data currently available. In the case of a mobile device it will use GPS first if enabled, then wi-fi.

    If native GPS is enabled javascript will access that data instead of the wi-fi data, but there is no way of preventing a check against the wi-fi data if the GPS data isn't available.

    Your best solution is to check the accuracy field and if it's not within a range you're happy with ask the user to enable GPS.

    Alternatively if you're building a hybrid app, most of the frameworks (PhoneGap .etc.) have APIs to query the device GPS directly. Use PhoneGap to Check if GPS is enabled