Last active
December 27, 2015 20:39
-
-
Save sjg/7386117 to your computer and use it in GitHub Desktop.
Parse CSV File
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
<script> | |
var mapOptions = { | |
center: new google.maps.LatLng(51.5, -0.11), | |
zoom: 10, | |
mapTypeId: google.maps.MapTypeId.TERRAIN | |
} | |
var getTweets; | |
$(function() { | |
console.log("Ready to do something"); | |
getTweets = function(){ | |
var map = new google.maps.Map(document.getElementById("map"), mapOptions); | |
var myStyle = []; | |
map.setOptions({styles: myStyle}); | |
$.get( "tweets.csv", function( data ) { | |
var csvline = data.split( "\n" ); | |
var lat, lng; | |
$.each(csvline, function(k,line){ | |
var s = line + ""; | |
var parseLine = s.splitCSV(); | |
try{ | |
if(parseLine[2] != undefined && parseLine[3] != undefined ){ | |
if(isNumber(parseLine[2])){ | |
console.log(parseLine[2] + "::" + parseLine[3]); | |
lat = parseLine[2]; | |
lng = parseLine[3]; | |
var text = parseLine[1]; | |
var image = 'http://media.stevenjamesgray.com/tweet.png'; | |
var latLng = new google.maps.LatLng(lat,lng); | |
var marker = new google.maps.Marker({ | |
position: latLng, | |
title: text, | |
icon: image | |
}); | |
marker.setMap(map); | |
}else{ | |
if(isNumber(parseLine[3])){ | |
console.log(parseLine[3] + "::" + parseLine[4]); | |
lat = parseLine[3]; | |
lng = parseLine[4]; | |
var text = parseLine[1] + parseLine[2]; | |
var latLng = new google.maps.LatLng(lat,lng); | |
var image = 'http://media.stevenjamesgray.com/tweet.png'; | |
var marker = new google.maps.Marker({ | |
position: latLng, | |
title: text, | |
icon: image | |
}); | |
marker.setMap(map); | |
} | |
}; | |
} | |
}catch(ex){ | |
} | |
}); | |
}); | |
}; | |
getTweets(); | |
setInterval(function(){ getTweets();}, 10 * 1000); | |
}); | |
String.prototype.splitCSV = function(sep) { | |
var regex = /(\s*'[^']+'|\s*[^,]+)(?=,|$)/g; | |
return matches = this.match(regex); | |
} | |
function isNumber(n) { | |
return !isNaN(parseFloat(n)) && isFinite(n); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment