Created
February 27, 2015 21:27
-
-
Save mapsam/2f2fe8dc9892ebaa0f1c to your computer and use it in GitHub Desktop.
Retrieve the Latitude and Longitude values from the Census geocoding API
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
<!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