HTML and CSS Reference
In-Depth Information
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
// print results
printLatLong(results[0].geometry.location.lat(),
results[0].geometry.location.lng());
} else {
error('Google did not return any results.');
}
} else {
error("Reverse Geocoding failed due to: " + status);
}
});
}
else {
error('Please enter an address');
}
});
});
// output lat and long
function printLatLong(lat, long) {
$('body').append('<p>Lat: ' + lat + '</p>');
$('body').append('<p>Long: ' + long + '</p>');
}
function error(msg) {
alert(msg);
}
Discussion
When the user clicks the button, use jQuery to read the value and validate that it's not
blank. Next, create an instance of the Geocoder object. In order to do so, call the geo
code() method, but pass an address option instead of latitude and longitude:
// set up the Geocoder object
var geocoder = new google.maps.Geocoder();
// return the coordinates
geocoder.geocode({ 'address': address }, function (results, status) {
...
Then access the geometry property of the GeocoderResults object. The geometry prop-
erty contains a location property that can then be used to call the lat and lng methods
to get our address's coordinates, which are then appended to the web page body in our
printLatLong() function:
// print results
printLatLong(results[0].geometry.location.lat(), results[0].geometry.location.lng());
 
Search WWH ::




Custom Search