Google Maps API - geocoding
by Frank van Puffelen
HTML
<script src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<input id="postcode" type="textbox" value="SO16 6AB">
<input id="geocodePostcode" type="button" value="postcode"><br/>
<div id="output"></div>
JavaScript
function geolocatePostcode(postcode, coordinates){
var geocoder = new google.maps.Geocoder();
var address = postcode + ', UK&sensor=false';
alert("Postcode = " + address);
geocoder.geocode(
{'address': address},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
alert("Geocode SUCCESSFUL");
coordinates([ results[0].geometry.location.lat(), results[0].geometry.location.lng() ]);
}
else {
alert("Geocode was not successful for the following reason: " + status);
coordinates(false);
}
}
);
}
document.getElementById( 'geocodePostcode' ).addEventListener( 'click', function () {
var postcode = document.getElementById( 'postcode' ).value;
geolocatePostcode( postcode, function ( output ) {
document.getElementById( 'output' ).textContent = output;
} );
}, false );