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!
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With