HTML and CSS Reference
In-Depth Information
Figure 8-1. The latitude and longitude coordinates determined natively through the browser
Start by adding an input button to the page:
<input type="button" id="go" value="Click Me To View Your Location" />
Next, add the following JavaScript to handle the button's click event, access the Geo-
location API, and output the results:
<script>
$(document).ready(function () {
// wire up button click
$('#go').click(function () {
// test for presence of geolocation
if (navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geo_success, geo_error);
} else {
error('Geolocation is not supported.');
}
});
});
function geo_success(position) {
printLatLong(position.coords.latitude, position.coords.longitude);
}
// The PositionError object returned contains the following attributes:
// code: a numeric response code
// PERMISSION_DENIED = 1
// POSITION_UNAVAILABLE = 2
// TIMEOUT = 3
// message: Primarily for debugging. It's recommended not to show this error
// to users.
function geo_error(err) {
if (err.code == 1) {
error('The user denied the request for location information.')
 
Search WWH ::




Custom Search