If you web application needs location of the user this is the code you want. But remember this is HTML 5 and won’t work with your old web browsers.
In most cases this gives the correct location of the user depends on the device capabilities. If user uses a mobile device GPS enabled this gives the exact location, but if it’s not it uses ip addresses and other signalling details to detect the location which not be very accurate.
<script type="text/javascript">
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
console.log('More or less ' + crd.accuracy + ' meters.');
};
function error(err) {
console.warn('ERROR(' + err.code + '): ' + err.message);
};
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(success, error, options);
</script>
Any way that’s all folks, have fun with you location enabled web applications.
Thanks