Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I read the 'real' mobile GPS coordinates from a website?

I did a website and I want to read the 'real' position GPS on mobile (Android & iPhone). When I try set the location on my website from my Android with W3C javascript method the GPS is not enabled and the position is set by IP (When I try with Google Maps app the GPS is enabled and blink on the status bar). Is any way for read the GPS (real GPS) from a web on a mobile? Thanks in advance!

like image 680
user512663 Avatar asked Apr 16 '12 18:04

user512663


1 Answers

With HTML5 you can do that. You need to check Geolocation API: Dive Into HTML5 and W3C Geolocation API Specification

The simplest example looks like:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition( 
        function (position) {  
            do_something(position.coords.latitude,position.coords.longitude);
        }, 
        function (error){
            switch(error.code){
                case error.TIMEOUT:
                    // Timeout
                    break;
                case error.POSITION_UNAVAILABLE:
                    // Position unavailable
                    break;
                case error.PERMISSION_DENIED:
                    // Permission denied
                    break;
                case error.UNKNOWN_ERROR:
                    // Unknown error
                    break;
                default: break;
           }
        }
    );
}

Probably you're also interesting in some browser specific implementations: Mozilla, IE, Chrome

Updated. As Mozilla said here, Devices with a GPS, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or wifi) may be returned to getCurrentPosition() to start.

So, if you need to have high accuracy (like GPS only), use watchPosition instead of getCurrentPosition.

like image 133
gakhov Avatar answered Oct 09 '22 15:10

gakhov