With HTML5 you can do this. You need to check the geolocation API: HTML5 Dive and W3C Geolocation API Specification
The simplest example looks like this:
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; } } ); }
You are probably also interested in some specific browser implementations: Mozilla , IE , Chrome
Updated. As Mozilla says here , GPS devices, for example, can take a minute or more to get a GPS fix, so less accurate data (IP location or Wi-Fi) can be returned to getCurrentPosition () to run.
So, if you need high accuracy (for example, only GPS), use watchPosition
instead of getCurrentPosition
.
source share