Get coordinates from Hardware GPS Get coordinates from Hardware GPS php php

Get coordinates from Hardware GPS


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


Geolocation API does not expose a direct way to check whether GPS is on or off, but you can catch the errors of geolocation and base on error type can draw conclusions from there.

E.g. POSITION_UNAVAILABLE (2) if the network is down or the positioning satellites can’t be contacted. But its not sure short way you have to handle some conditions!

I will suggest use watchPostion { i agree its meant to watch and continuous to locate position} u can keep it on and if GPS throw the error u can prompt custom alert to make user turn on the GPS device/wifi/internet .. and if its come to success callback u can clear the watch.

 var watch =null; function success(position){   var lat = position.coords.latitude;   var lon= position.coords.longitude;   if (watch != null )  /*Need to take care .. as maybe there is no gps and user   want it off so keep attempt 3 times or some kind a way out otherwise it will   infinite loop */    {        navigator.geolocation.clearWatch(watch);        watch = null;    }}function getLatLon(){    var geolocOK = ("geolocation" in navigator);    if ( geolocOK )     {        var option = {enableHighAccuracy:true, maximumAge: 0,timeout:10000 };        watch =  navigator.geolocation.watchPosition(success, fails,  option);    }    else {        //disable the current location?      }}function fails(){    alert("please turn on the GPS !");}getLatLon();