Skip to content

Instantly share code, notes, and snippets.

@mapsam
Created February 27, 2015 21:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapsam/2f2fe8dc9892ebaa0f1c to your computer and use it in GitHub Desktop.
Save mapsam/2f2fe8dc9892ebaa0f1c to your computer and use it in GitHub Desktop.
Retrieve the Latitude and Longitude values from the Census geocoding API
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<style>
body {
background: MediumAquaMarine;
}
</style>
</head>
<body>
<input type="text" id="address">
<input type="submit" value="GEOCODE!" onclick="geocode();">
<script>
function geocode() {
var address = document.getElementById('address').value;
$.ajax({
url: 'http://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address='+address+'&benchmark=4&format=jsonp',
dataType: 'jsonp',
success: function(response) {
// using the response, we can grab the latitude and longitude
// using javascript's dot syntax to cyphen through the returned
// json object
var latitude = response.result.addressMatches[0].coordinates.x;
var longitude = response.result.addressMatches[0].coordinates.y;
console.log('Latitude: ' + latitude);
console.log('Longitude: ' + longitude);
},
error: function(error) {
console.log(error);
}
});
return false;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment