HTML and CSS Reference
In-Depth Information
see if the position is unavailable for some other reason or if the user does not respond in a
timely manner.
A function that gets the latitude and longitude by calling the
coords.latitude()
and coords.longitude() functions, respectively, for the object returned from the
geolocation.getCurrentPosition() function.
Here's some sample code that meets all the geolocation criteria to return the user's latitude and
longitude:
<script>
function initiate_geolocation() {
navigator.geolocation.getCurrentPosition
(handle_geolocation_query,handle_errors);
}
function handle_errors(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED: alert(“user did not share geolocation data”);
break;
case error.POSITION_UNAVAILABLE: alert(“could not detect current position”);
break;
case error.TIMEOUT: alert(“retrieving position timed out”);
break;
default: alert(“unknown error”);
break;
}
}
function handle_geolocation_query(position){
alert('Lat: ' + position.coords.latitude +
' Lon: ' + position.coords.latitude);
}
</script>
Once you have the user's position in the form of the latitude and longitude, you can use those details
to map the location or locate services and businesses in the area.
Browsers that currently support geolocation include:
Internet Explorer 9.0 (beta)
Firefox 3.5+
Safari 5.0+
Google Chrome 5.0+
Opera 10.6+
Search WWH ::




Custom Search