Using jQuery to Lookup Polling Places via Google
Using https://developers.google.com/civic-information/ to pull polling places.
by chrislkeller
HTML
<h2>Hey find your polling place</h2>
<input placeholder="Enter Yer Address" ><br />
<button>Get Some</button>
<hr>
<h2 style="display: none;" >Polling Locations</h2>
<ul></ul>
<h2 style="display: none;" >Early Vote</h2>
<ul></ul>
CSS
body { font-family: Tahoma; color: #333; margin: 10px; }
input { width: 300px; }
ul, ul li { list-style: disc; font-size: 10px; margin: 10px 20px; }
JavaScript
function pollingPlace(address, callback) {
// This is the call to get the API info
var key = 'AIzaSyDHwRG5yky13wkdU04kA_hBgEc-Yg9WhUI'
$.ajax({
url: 'https://www.googleapis.com/civicinfo/us_v1/voterinfo/4000/lookup?key='+key+'&officialOnly=false',
type: 'POST',
contentType: 'application/json',
data: '{ "address": "'+address+'" }',
success: function(response) { callback(response) }
})
}
function processLocations(locations ) {
// This processes election locations into something more manageable
var locales = []
if( typeof locations == 'undefined' ) return locales;
for( var i = 0; i < locations.length; i++ ) {
var locale = locations[i],
address = [
locale.address.line1,
locale.address.line2,
locale.address.line3,
locale.address.city,
locale.address.state,
locale.address.zip
].join(' '),
directions = 'https://maps.google.com/maps?q=from:'+youraddress+' to:'+address,
hours = [
locale.pollingHours ? 'From: '+locale.pollingHours : '',
locale.startDate ? 'on '+locale.startDate : '' ,
locale.endDate ? 'till '+locale.endDate : ''
].join(' ').trim()
locales.push( {
name: locale.address.locationName,
address: address,
hours: hours,
directions: directions });
}
return locales;
}
function printLocaiton( $list, locations ) {
// This addes the location objects to the DOM
$list.html('').prev('h2').show();
for( var i = 0; i < locations.length; i++ ) {
var location = locations[i];
$list.append( [ '<li><strong>',location.name,
'</strong> at <a href="',
location.directions,'" target="_blank">',
location.address,
...