Created
December 18, 2011 21:58
-
-
Save shaneshifflett/1494607 to your computer and use it in GitHub Desktop.
Fusion Tables data source: intersect a shape file, get key, lookup value in related table
This file contains 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
function findAddr(){ | |
var address = $("#search_text").val() | |
geocoder.geocode({ 'address': address }, function (results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
if(cur_marker != null){ | |
cur_marker.setMap(null); | |
} | |
//Create the Map and center to geocode results latlong | |
var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()); | |
var queryText = encodeURIComponent("SELECT 'PRECNAME' FROM "+precinct_ft+" WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG("+latlng.lat()+","+latlng.lng()+"),1))"); | |
var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + queryText); | |
//SECRETE SAUCE, callback getData function when we have a response from Google with our rows | |
query.send(getData); | |
cur_marker = new google.maps.Marker({ | |
position: latlng, | |
map: map, | |
}); | |
map.setCenter(latlng); | |
map.setZoom(14); | |
}else{ | |
alert("We couldn't locate your address. Please ensure you entered your street number, street, city, state and zip then try again."); | |
return false; | |
} | |
}); | |
} | |
function getData(response){ | |
//get a reference to the precinct shape file fusion table | |
var data_table = response.getDataTable(); | |
try{ | |
//get the first cell from the table (precinct name) | |
var prec = data_table.getValue(0,0); | |
if(layer != null){ | |
layer.setMap(null); | |
} | |
//use Gmap's Fusion Tables api to look up the corresponding polling place and plot it on the map | |
layer = new google.maps.FusionTablesLayer(ft_id,{ | |
query: "SELECT 'Location 2', 'Location1' FROM "+ft_id+" WHERE Precinct CONTAINS '"+prec+"'" | |
}); | |
layer.setOptions({suppressInfoWindows: true}); | |
layer.setMap(map); | |
layerListener(layer); | |
var queryText = encodeURIComponent("SELECT 'Location 2', 'Location1', 'Precinct', 'Direction' FROM "+ft_id+" WHERE Precinct CONTAINS '"+prec+"'"); | |
//additional query using visualization's api so we can update the UI with data about the polling place | |
var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + queryText); | |
query.send(updateSelectedLocation); | |
$("#reset").show(); | |
} catch(err){ | |
alert("We couldn't locate your address. Please ensure you entered your street number, street, city, state and zip then try again."); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment