Skip to content

Instantly share code, notes, and snippets.

@alexjamesbrown
Created June 18, 2013 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexjamesbrown/5803893 to your computer and use it in GitHub Desktop.
Save alexjamesbrown/5803893 to your computer and use it in GitHub Desktop.
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var geocoder;
$(function () {
geocoder = new google.maps.Geocoder();
$('#searchBtn').click(function () {
geoCode($("#locationValue").val(), function (latLon) {
$('#lat').val(latLon.Lat);
$('#lon').val(latLon.Lon);
$('#searchForm').submit();
});
});
});
function geoCode(postcode, cb) {
geocoder.geocode({ 'address': postcode }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location
.toString()
.replace(' ', '')
.replace('(', '')
.replace(')', '')
.split(',');
cb({ Lat: latLng[0], Lon: latLng[1] });
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment