Created
September 23, 2019 01:54
-
-
Save kenmasters/63858d2b4a0679257f17359830d14e1d to your computer and use it in GitHub Desktop.
Geocoding address using google
This file contains hidden or 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
//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