HTML 5 (GEOLOCATION)

by isogunro

HTML

<div id="message"></div>

JavaScript

$(function () {
	getLocation();
});

function supportsGeolocation() {
    //Checks if browser supports geolocation
	return 'geolocation' in navigator;
}

function showMessage(message) {
	$('#message').html(message);
}

function getLocation() {
    //The call to check if geolocation supported by browser
	if (supportsGeolocation()) {
        //getCurrentPosition() gets the current position of the device in which the application is running
        //parameters of getCurrentPosition() are position call back and error Position callback functions
		navigator.geolocation.getCurrentPosition(showPosition, errorPosition);
	}
	else {
		showMessage("Geolocation is not supported by this browser.");
	}
}

function errorPosition(err) {	
showMessage("error: " + err.message + " code: " + err.code);
}


function showPosition(position) {
	var datetime = new Date(position.timestamp).toLocaleString();
	showMessage("Latitude: "+position.coords.latitude + "<br />"
	+ "Longitude: "+position.coords.longitude + "<br />"
	+ "Timestamp: "+datetime);
}