Created
April 6, 2012 01:53
-
-
Save developit/2315951 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> | |
<script type='text/javascript'> | |
/** Get the element with the given id */ | |
function el(id) { return document.getElementById(id); } | |
window.onload = function() { | |
var geocoder = new google.maps.Geocoder(), | |
output = el('output'); | |
/** Handle form submission */ | |
el('addressForm').onsubmit = function() { | |
var address = el('address').value; | |
geocodeAddress(address, function(lat, lng) { | |
var lines = [ | |
'Address: ' + address, | |
'Lat: ' + lat, | |
'Lng: ' + lng, | |
'\n' | |
]; | |
output.value = (output.value || '') + lines.join('\n'); | |
}); | |
return false; | |
}; | |
/** Geocode an address. Callback gets passed (lat, lng). */ | |
function geocodeAddress(address, callback) { | |
geocoder.geocode({ address:address }, function(results, status) { | |
var loc; | |
if (status==google.maps.GeocoderStatus.OK) { | |
loc = results[0].geometry.location; | |
callback(loc.lat(), loc.lng()); | |
} | |
}); | |
} | |
}; | |
</script> | |
</head> | |
<body> | |
<form id="addressForm" style="margin:10px 0;"> | |
<input id="address" name="address" size="69" value="Whitehouse, Washington DC, USA"><br /> | |
<input type="submit" value="Submit"/> | |
</form> | |
<textarea id="output" style="width:400px; height:200px; font-family:'Lucida Console', Monaco, monospace;"></textarea> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment