Skip to content

Instantly share code, notes, and snippets.

@mhawksey mhawksey/gist:8587272
Last active Jan 4, 2016

Embed
What would you like to do?
function cacheMap(){
var what = "#rhizo14";
var openTag = '<?xml version="1.0" encoding="UTF-8"?> '+
' <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">' +
' <Document>' +
' <name><![CDATA[Twitter '+what+' Contributor Map]]></name>' +
' <description><![CDATA[Map of Twitter accounts that have used the '+what+' hashtag which include location data. Markers are listed by number of tweets in the archive ]]></description>';
var output = [[openTag]];
var db = ScriptDb.getMyDb();
var counter = 0;
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("CacheMap");
var data = sheet.getRange(2, 4, sheet.getLastRow(), 4).getValues();
for (var r = 0; r < data.length; r++){
if (data[r][0] !="" && counter < 100){
var results = db.query({user_location: data[r][0]});
if (results.hasNext()){
var item = results.next();
if (item.status !== "ZERO_RESULTS")
output.push([formatPlacemark(data[r][2], item.location, data[r][1], data[r][3])]);
} else {
var maps = Maps.newGeocoder();
Logger.log("%s - %s: %s", r, data[r][1], data[r][0]);
var response = maps.geocode(data[r][0]);
Utilities.sleep(1000);
var resp = {status: response.status, user_location: data[r][0]};
if (response.status !== "ZERO_RESULTS"){
var result = response.results[0];
resp.location = {lat: result.geometry.location.lat,lng: result.geometry.location.lng};
output.push([formatPlacemark(data[r][2], result.geometry.location, data[r][1], data[r][3])]);
counter++;
}
db.save(resp);
}
}
}
output.push(['</Document></kml>']);
doc.getSheetByName("CacheMap").getRange(1, 1, output.length).setValues(output);
}
function formatPlacemark(handle, loc, desc, imagesrc){
return '<Placemark>' +
'<name>'+handle+'</name>' +
'<Point><coordinates>'+loc.lng +','+loc.lat+'</coordinates></Point>' +
'<description><![CDATA[ ' +
'<img alt="'+handle+'" src="'+imagesrc+'" style="float:left; padding: 0 3px 3px" /> ' +
desc +'<br/>' +
'<a href="https://twitter.com/'+handle+'">View Profile Twitter</a> | <a href="http://hawksey.info/tagsexplorer/?key=tDHgQeSbHR3fcvN81EHnxQQ&sheet=oaw&gqw=lower(C)%20contains%27'+handle.toLowerCase()+'%27%20OR%20lower(B)%3D%27'+handle.toLowerCase()+'%27">View #rhizo14 convo graph</a>' +
'<hr style="clear:both"/>' +
']]>' +
'</description>' +
'</Placemark>'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.