Skip to content

Instantly share code, notes, and snippets.

@viirya
Last active December 14, 2015 11:48
Show Gist options
  • Save viirya/5081476 to your computer and use it in GitHub Desktop.
Save viirya/5081476 to your computer and use it in GitHub Desktop.
Simple Perl script used to encode processed tweets for visualizing on Google Map.
use strict;
use JSON;
use Data::Dumper;
open(TWEET_STAT, "<$ARGV[0]");
my $rows = [];
while (<TWEET_STAT>) {
if ($_ =~ /(.*?)\x01(.*?)\x01(\d*)/) {
my $lat = $1 + 0;
my $lng = $2 + 0;
my $tweet_count = $3 + 0;
last if ($tweet_count <= 50);
push @{$rows}, {"coordinates" => [$lng, $lat], "tweet_count" => $tweet_count}
}
}
my $results = {"rows" => $rows};
my $json = encode_json $results;
$json = "eqfeed_callback($json);";
print $json;
close(TWEET_STAT);
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization"></script>
<style>
.container { height: 400pt; padding: 0 20pt 0 20pt; }
#map_canvas { height: 85%; }
</style>
<script type="text/javascript">
function init() {
var mapOptions = {
zoom: 2,
center: new google.maps.LatLng(2.8, -187.3),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var script = document.createElement('script');
script.src = 'tweets_heatdata';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);
window.eqfeed_callback = function(results) {
var heatmapData = [];
for (var i = 0; i < results.rows.length; i++) {
var coords = results.rows[i].coordinates;
var latLng = new google.maps.LatLng(coords[1], coords[0]);
var count = results.rows[i].tweet_count;
var weightedLoc = {
location: latLng,
weight: count
};
heatmapData.push(weightedLoc);
}
var heatmap = new google.maps.visualization.HeatmapLayer({
data: heatmapData,
dissipating: false,
map: map
});
}
}
google.maps.event.addDomListener(window, "load", init);
</script>
</head>
<body>
<div class="container">
<div id="map_canvas"></div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment