Skip to content

Instantly share code, notes, and snippets.

@knorthfield
Created March 11, 2013 15:54
Show Gist options
  • Save knorthfield/5135236 to your computer and use it in GitHub Desktop.
Save knorthfield/5135236 to your computer and use it in GitHub Desktop.
Get Address from UK Postcode
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Get Address from Postcode</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var geocoder;
function codeAddress() {
geocoder = new google.maps.Geocoder();
var postcode = document.getElementById("postcode").value;
geocoder.geocode( { 'address': postcode, 'region': 'uk' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
geocoder.geocode( { 'latLng': results[0].geometry.location, 'region': 'uk' }, function(result, status) {
if (status == google.maps.GeocoderStatus.OK) {
document.getElementById('address').value =
document.getElementById('house_number').value + ' ' +
result[0]['address_components'][1]['long_name'] + '\n'
+ result[0]['address_components'][2]['long_name'] + '\n'
+ result[0]['address_components'][3]['long_name'] + '\n'
+ document.getElementById('postcode').value.toUpperCase();
} else {
alert("Converting LatLng to Address failed: " + status);
}
});
} else {
alert("Converting Postcode to LatLng failed: " + status);
}
});
}
</script>
</head>
<body>
<div>
<label>
House Number <input id="house_number" type="text">
</label>
<br>
<label>
Postcode <input id="postcode" type="text">
</label>
<input type="button" value="Find Address" onclick="codeAddress()">
</div>
<textarea name="final_address" id="address" cols="30" rows="10"></textarea>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment