user512663
user512663

Reputation: 173

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!

Upvotes: 6

Views: 5458

Answers (2)

Loufylouf
Loufylouf

Reputation: 699

As stated in the W3C specification, you might need to use the third argument of both watchPosition and getCurrentPosition, which is a PositionOptions interface to set enableHighAccuracy to true, else, using its default value false, the GPS won't necessarily be switched on by the device.

Upvotes: 0

gakhov
gakhov

Reputation: 1971

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.

Upvotes: 7

Related Questions