Created
February 27, 2015 21:17
-
-
Save mapsam/da2d6e700563b360948d to your computer and use it in GitHub Desktop.
This example allows the user to enter an address and pass the value to a function that calls 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: salmon; | |
} | |
</style> | |
</head> | |
<body> | |
<input type="text" id="address"> | |
<input type="submit" value="GEOCODE!" onclick="geocode();"> | |
<script> | |
// this function is called when the user presses the | |
// submit button above | |
function geocode() { | |
// using document.getElementById('address'), we can access | |
// the current value inside of that element that the use has entered. | |
// Take the value and assign it to the variable "address" so we can | |
// us it in the HTTP string to the Census | |
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) { | |
console.log(response); | |
}, | |
error: function(error) { | |
console.log(error); | |
} | |
}); | |
// this prevents the page from reloading when the users presses | |
// the submit button. | |
return false; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment