Skip to content

Instantly share code, notes, and snippets.

@kenmasters
Created September 23, 2019 01:54
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 kenmasters/63858d2b4a0679257f17359830d14e1d to your computer and use it in GitHub Desktop.
Save kenmasters/63858d2b4a0679257f17359830d14e1d to your computer and use it in GitHub Desktop.
Geocoding address using google
//Geocode function for the origin location
function GoogleGeocode() {
geocoder = new google.maps.Geocoder();
this.geocode = function(address, callbackFunction) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var result = {};
result.latitude = results[0].geometry.location.lat();
result.longitude = results[0].geometry.location.lng();
callbackFunction(result);
} else {
alert("Geocode was not successful for the following reason: " + status);
callbackFunction(null);
}
});
};
}
//Process form input
$(function() {
$('#user-location').on('submit', function(e){
//Stop the form submission
e.preventDefault();
//Get the user input and use it
var userinput = $('form #address').val();
if (userinput == "")
{
alert("The input box was blank.");
}
var g = new GoogleGeocode();
var address = userinput;
g.geocode(address, function(data) {
if(data != null) {
olat = data.latitude;
olng = data.longitude;
$('#geocode-result').append("Latitude: " + olat + "<br />" + "Longitude: " + olng + "<br /><br />");
} else {
//Unable to geocode
alert('ERROR! Unable to geocode address');
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment