Wednesday, February 22, 2012

Get device geolocation, using window.navigator.geolocation

It's a html example to get device geolocation, using window.navigator.geolocation API, run on Opera Mobile on Android mobile. Before the browser can get device geolocation, user will be prompted to confirm share location with browser.
Confirm share location
Get device geolocation, using window.navigator.geolocation
<!DOCTYPE html>
<html>
<head>
<title>Mobile-Web-App: geolocation</title>

<script type="text/javascript">

if (window.navigator.geolocation) {
window.navigator.geolocation.getCurrentPosition(
successCallback, errorCallback);
} else {
alert("!!!Browser doesn't support Geolocation API.");
}

function successCallback(position){

document.getElementById("latitude").innerHTML
= "latitude: " + position.coords.latitude;
document.getElementById("longitude").innerHTML
= "longitude: " + position.coords.longitude;
document.getElementById("accuracy").innerHTML
= "accuracy: " + position.coords.accuracy;
}

function errorCallback(error) {
alert(error);
}

</script>

</head>
<body>
<h1>Mobile-Web-App: geolocation</h1>
<div>
<p id="latitude"></p>
<p id="longitude"></p>
<p id="accuracy"></p>
</div>
</body>
</html>


No comments:

Post a Comment