Skip to content

Instantly share code, notes, and snippets.

@cdevroe
Created December 27, 2009 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdevroe/264161 to your computer and use it in GitHub Desktop.
Save cdevroe/264161 to your computer and use it in GitHub Desktop.
<?php
/*
Tweet Maps
by Colin Devroe
Created on December 26th, 2009
while playing New Super Mario Bros. Wii.
Tweet Maps objective is to take any username's
tweets and place them onto a Google Map.
*/
$statusEndpoint = 'http://twitter.com/statuses/user_timeline/';
$twitter = array('username' => $_GET['username'], 'password' => '');
$googleMapsAPIkey = "";
require_once('coreylib.php');
$api = new clAPI($statusEndpoint.$twitter['username'].'.xml?count=100'); $api->parse('10 minutes');
/**
* @param $text The text of a Twitter status update
* @return The status with typical linking to users and URLs
*/
function tweet($text) {
//$text = preg_replace('#http://[^ ]+#i', '<a href="\\0">\\0</a>', $text);
$text = preg_replace('#http://twitpic.com/([a-z0-9_]+)#i', '<span class="thumbnail"><a href="http://twitpic.com/\\1"><img width="60" src="http://twitpic.com/show/mini/\\1" /></a></span>', $text);
$text = preg_replace('#http://yfrog.com/([a-z0-9_]+)#i', '<span class="thumbnail"><a href="http://yfrog.com/\\1"><img width="60" src="http://yfrog.com/\\1.th.jpg" /></a></span>', $text);
$text = preg_replace('#http://yfrog.us/([a-z0-9_]+)#i', '<span class="thumbnail video"><a href="http://yfrog.us/\\1"><img width="60" src="http://yfrog.us/\\1:frame" /></a></span>', $text);
$text = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/\\1">\\0</a>', $text);
return $text;
}
// Get the LATEST tweet and create a list of Tweets
// foreach status update in the feed
//$tweetlist = array();
foreach($api->get('status') as $status) {
if ($status->get('geo/georss:point') == '') {
continue;
} else {
$geo = explode(" ", $status->get('geo/georss:point'));
$tweetlist[] = array('author'=>$status->get('user/screen_name'),'id'=>$status->get('id'),'url'=>'http://twitter.com/'.$status->get('user/screen_name').'/statuses'.$status->get('id'),'tweet'=>tweet($status->get('text')),'lat'=>$geo[0],'long'=>$geo[1]);
}
}
if (!$tweetlist) {
Header("Location: http://cdevroe.com/tweetmaps/nogeo.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Tweet Maps by Colin Devroe</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=GOOGLEMAPSKEYHERE"
type="text/javascript"></script>
<script type="text/javascript">
var map;
var messages = [];
<?php for ($i=0;$i<count($tweetlist);$i++) {
echo "messages[".$i."] = [];"."\n";
echo "messages[".$i."]['author'] = '".$tweetlist[$i]['author']."';"."\n";
echo "messages[".$i."]['id'] = '".$tweetlist[$i]['id']."';"."\n";
echo "messages[".$i."]['url'] = 'http://twitter.com/".$tweetlist[$i]['author']."/statuses/".$tweetlist[$i]['id']."';"."\n";
echo "messages[".$i."]['tweet'] = '".addslashes($tweetlist[$i]['tweet'])."';"."\n";
echo "messages[".$i."]['lat'] = '".$tweetlist[$i]['lat']."';"."\n";
echo "messages[".$i."]['long'] = '".$tweetlist[$i]['long']."';"."\n"."\n";
} ?>
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(messages[0]['lat'],messages[0]['long']), 10);
map.addControl(new GSmallMapControl());
map.openInfoWindowHtml(map.getCenter(),messages[0]['tweet'],{maxWidth:200});
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
for (var i=0;i<<?php echo count($tweetlist)?>;i++) {
/*var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
southWest.lng() + lngSpan * Math.random());*/
var point = new GLatLng(messages[i]['lat'],messages[i]['long']);
map.addOverlay(createMarker(point,i));
}
//map.setUIToDefault();
}
}
function createMarker(latlng, number) {
var blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.image = "http://twivatar.org/<?php echo $twitter['username']; ?>/mini";
blueIcon.iconSize = new GSize(24, 24);
var marker = new GMarker(latlng,{icon:blueIcon});
marker.value = number;
GEvent.addListener(marker,"click", function() {
var myHtml = messages[number]['tweet'];
map.openInfoWindowHtml(latlng, myHtml, {maxWidth:200});
});
return marker;
}
function showTweet(number) {
map.panTo(new GLatLng(messages[number]['lat'], messages[number]['long']));
var myHtml = messages[number]['tweet'];
var point = new GLatLng(messages[number]['lat'],messages[number]['long']);
map.openInfoWindowHtml(point, myHtml, {maxWidth:200});
$('#tweets ul li').removeClass('selected');
$('#tweet-'+messages[number]['id']).addClass('selected');
}
</script>
<style type="text/css">
body {
font-family:arial, verdana, tahoma, sans-serif;
font-size: 12px;
}
#content { width: 950px; }
#tweets { float: right; }
#tweets h2 {text-align: right;}
#tweets ul { list-style: none; margin: 0; width: 300px; height: 400px; overflow-y: scroll; }
.thumbnail { margin: 5px 2px; }
.thumbnail.video { border-bottom: 3px solid maroon; }
.selected { background: #FDFFD9;}
#footer {clear: both;}
</style>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="content">
<div id="tweets">
<h2><img src="http://twivatar.org/<?php echo $twitter['username']; ?>/" /> <?php echo $twitter['username']; ?></h2>
<ul>
<?php for($i=0;$i<count($tweetlist);$i++) { ?>
<li id="tweet-<?php echo $tweetlist[$i]['id']; ?>" <?php if ($i==0) { echo 'class="selected"'; } ?>><a href="#" onclick="showTweet('<?php echo $i; ?>'); return false;"><img src="images/map_magnify.png" width="16" height="16" alt="Map Magnify"></a> <?php echo $tweetlist[$i]['tweet']; ?></li>
<?php } ?>
</ul>
</div>
<div id="map_canvas" style="width: 600px; height: 400px"></div>
<div id="footer"><a href="http://cdevroe.com/tweetmaps/">Tweet Maps</a> is by <a href="http://cdevroe.com/">Colin Devroe</a>.</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment