Nowadays Twitter is changing how the web is used. In fact under the pressure of the twitter’s FireHose even Google Search is starting to show transient informations, i.e. the tweets (cfr. Google Tweets and updates and search). As everybody knows a tweet is a short text message (less than 140 characters) and it is intended to deliver ’short living’ messages:
Furthermore, on Nov. 19 2009, Twitter turned on geo-location functionality and started to give the possibility to external applications to provide geo-tagging features to end user: “We are really looking forward to seeing the creative uses emerge from the developer community,” (Ryan Sarver , twitter official blog) and on Dec. 23 2009 Twitter, on the official blog, announced a major new step into the location-aware future, i.e. The acquisition of Mixer Labs, creators of GeoAPI.
This new opportunity is increasing enormously the number of geo-located tweets that can be easily accessed by the twitter search API and give rise to tons of “funny games” with tweets.
The basic one is to show where tweets are coming from. Our today’s pet project is exactly showing tweets on a Google map (almost) continuously. Test it here.
Getting Tweets
Twitter exposes its data via a combination of at least two Application Programming Interfaces (APIs) but for our purpose the simple and yet powerful Search API (rigurously the search method of the RESTful Search API) is largely sufficient. What we need to sample tweets in a given region, say 10km around my home, is to access the URL:
http://search.twitter.com/search.json?geocode=[LAT]%2C[LNG]%2C10kmwhere, of course, [LAT] and [LNG] have to be replaced with the actual coordinates of my home. The call do not require authentication and, as explained in the official documentation there are a lot of criteria you can use to get a specific set of tweets (for example include q=XYZ to get the tweets that contain the word XYZ). Notably the shown request will bring the selected tweets as a JSON (www.json.org) object so trivially usable in javascript.
Unfortunately the direct call to the url in javascript does not work, at least if you are not able to host your page on twitter.com domain. In fact the so called Same Origin Policy (SOP) that is in some sense the heart of the security model of web browsers will not let you to execute programmatically a AJAX (XMLHttp,actually) request to a page whose domain is not the one the page has been loaded from, i.e. a javascript script hosted on www.my_home_site.com cannot request content from www.a_different_domain.com’s site. SOP is intended to limit the possibility of ‘malicious code injection’ and cannot be avoided, but can be in some sense mitigated with some tricks.
The simpler approach to circunvent SOP is named JSONP and is simple to apply but requires some help from the server, essentially the response must contain a function call that our application will execute on return (and this is exactly cone injection !): essentially to return the json object {'a' : true, 'b' : false} the server should include an enclosing a function call:
function_my_app_implements({'a' : true, 'b' : false})The good news is that the twitter search method has exactly this feature and just appending to the request &callback= function_my_app_implements we’ll get back the desired JSONP response. So we can write down a simple Jacascript function as:
loadFromTwitter = function(lat,lng,r) {
var url='http://search.twitter.com/search.json?geocode='
+lat+'%2C'
+lng+'%2C'+r
+'km&callback=manage_response
var script = document.createElement('script');
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
}and when the request finishes manage_response will be called with as argument an object containing an array of tweets (unfortunately no error control here, but there are different techniques with better control, even if less straightforward); just to show how manage_response may look like:
manage_response = function(tl) {
var results = tl.results;
for(var i =0; i< results.length;i++) {
do_something_meaningful_with_a_tweet(tw);
}
}Putting Tweets on the Google Maps
Once the tweets reach our javascript application, showing them on the map is a simple game using the Google Map API (take a look at the Google Maps documentation). Each tweet has a lot of informations (for example: from_user, text, source, …) but most notably for our purposes the tweets returned with a request containing geocode=... param has a location element, so is straightforward to write down a javascript function in order to put the marker on the map:
function createMarker(tweet) {
var location = tweet.location;
var point = getPointFromLocation(location);
var marker = new GMarker(point,{icon:twitterIcon});
GEvent.addListener(marker, function() {
var src = '
<div class="tweet_text">' +
' <img src="' + tw.profile_image_url +' " alt="" width="30px" height="30px" />' +
'<span class="twitter_marker">' + tweet.text +
'</span></div>
';
marker.openInfoWindowHtml(str);
});
return marker;
}where the added listener opens the GInfoWindow on the marker showing the current tweet; for instance the profile image of the author and the text of the tweet in the sample code. Once the marker is defined, we can put it on the map using the addOverlay(overlay:GOverlay map’s method, where the marker is the overlay we are going to add in our map.
As the Twitter official documentation says: “The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile”. This means that a tweet location may be given in a few of different formats, for example using coordinates (latitude and longitude) or by address. In the above code the function getPointFromLocation(location) has so to process the location field of the tweet; in the better case, it simply splits the coordinates and returns the LatLng point, while if the location is an address the function has to use the GClientGeocoder in order to request to Google servers coordinates for the user specified location text. Actually the above snippet is too simple to handle geolocation. Indeed as any server request geolocation (namely GclientGeocoder.getLatLng) is asyncronous so the code has to be refactored such that it searches (we do it using regular expressions) for latitude and longitude in the location field and if coordinates are found we create the marker directly, if there are not coordinates ready to be used it has to call the geocoder and eventually, upon successful return, create the marker:
….
geocoder.getLatLng(tweet.location, function(point) {
if(point) {
addOverlay(createMarker(tweet,point));
}
});
…..Finally, the actual code has to be a bit smarter because geolocation is a time consuming operation and many tweets have the same location, so caching is a must and finally because many tweets may and up at the same coordinates so the infowindow content has to be composed by multiple tweets (and pagination become unavoidable too), but this is out of our intent here.
Continuously
The tweet stream is continuous, so an application dealing with twitter have to be “fresh” in order to show what is appenning in the world. For this purpose javascript setInterval function results in an obvious choice giving us the possibility to call periodically loadFromTwitter; the latitude, longitude and radius are chosen on the basis of the current view of the shown map:
setInterval("periodicLoader()",1000);
periodicLoader = function() {
var c = map.getCenter();
var bnd = map.getBounds();
var sw = bnd.getSouthWest();
var ne = bnd.getNorthEast();
var radius = Math.floor( (sw.distanceFrom(ne)/2.0)*0.8 );
loadFromTwitter(c.lat(),c.lng(), radius);
}being map our global GMap instance.
Let me note that Tweetter provides an experimental stream access to tweets (a sampling of the firehose right now) that may be the right coice for receiving updates on the map continuously but the streaming api has not anonymous access and handling authentication in the sample code is going to complicate things a lot; on the other hand i think i will not resist the temptation of trying, ASAP, to use the stream, being this a fantastic project where to use HTML5 web-workers :).
This work is realized jointly with Alberto Mancini.
by
I am a researcher and co-founder of KeyChain and my current project is JooinK.
I am one of the founders of the firenze-GTUG.
I am an italian mathematician (Msc Univ. Camerino, PhD Univ. Firenze) and I worked on the development of a CFD code for numerical simulation (Numidia jointly with Ferrari - Rome; ANSYS Germany on the meshing Team - Hannover).


December 26, 2009 at 6:40 am
Brilliant post on geolocation services combining Twitter and Google. I’m looking forward to lots of geoservices coming online in 2010.
December 26, 2009 at 2:08 pm
Very interesting post, as usual :-)
December 27, 2009 at 2:34 am
absolutely stunning… twitter is soooooooo good and so is the team at woorkup.com :)
December 27, 2009 at 4:41 am
I have actually made a mashup like this a few months back, combing twitter, Google Maps and the Meetup.com API.
So basically it will get your position using the W3C Geolocation API, and using GeoNames, will get the city name. It will show you a map of where you are using Google Maps, and display all tweets mentioning the city you are in, as well as display all the events listed on Meetup.com happening nearby.
http://experimenting.in/other/demo_geo_twitter_mashup.htm
December 28, 2009 at 5:54 am
Interesting application.
Actually Twitter’s search API opens a wide range of possibilities and your is a cute one.
Our main purpose was just playing with “twitter geo” and we
chosen not to invest on “W3C Geolocation” to get the initial position
on the map beacuse google api loader appears largely enough
if you intend to use google maps.
Francesca
December 27, 2009 at 9:41 am
Useful post …. thank you so much :-)
December 27, 2009 at 1:52 pm
great tips…maybe useful for USA. Position in other countries is so difficult to find.
December 27, 2009 at 10:20 pm
Nice post :) ..tosi
December 28, 2009 at 4:50 am
No need to say that very nice post again and again….. :)
Thanks,
Harry
December 28, 2009 at 5:55 am
Thank you very much. :-)
We’re finishing right now another post on maps;
I hope you’ll enjoy.
Francesca.
December 28, 2009 at 10:11 am
awesome tutorials and i’m always looking such posts from woorkup
December 28, 2009 at 11:07 am
this might b interesting for the real estate industry, I guess. With what you described you could show all the twits during a certain timeperiode, around each individual listing. This way propects can ‘meet’ their futur neighbours.
Greets from the Netherlands,
boris
January 4, 2010 at 3:16 pm
Boris, we actually did exactly that here at Goomzee in the US (for real estate, tracking tweets, etc.). We have team in EU too so if you have any ideas on how best this would apply in your market(s), keep the great ideas flowing.
– Mike
December 28, 2009 at 11:39 pm
wew I wish there’s a generous people provides a downloadable working code to play around with it…
December 29, 2009 at 8:36 am
Hi,
actually the code IS available but let me say a couple of things:
1. the code’s style is a little bit ugly being written as an excerpt of JooinK, our still-in-progress map application.
2. all the code is in a single file (http://www.jooink.com/experiments/map03/index.html) save it and you can play with it as you want
3. the ’search area circle’ that appears on the map is realized using eshapes.js (http://econym.org.uk/gmap/eshapes.htm)
Enjoy playing with the sample code of the JooinK-team :-)
December 29, 2009 at 5:29 am
Good explanation on how the twitter’s Geo location functionality work. Great work! Your post is very informative. Thanks.
December 30, 2009 at 8:34 am
[...] definitely think that maps are the today’s cooler topic, as told in another post (Play with Google maps and Twitter api), and Google Maps API exposes so many functionalities so that I and Alberto Mancini decided to [...]
January 3, 2010 at 8:44 am
That is a very good article. Amazing. I am sure it will spur good sites on geo location
Thank you Francesca
January 4, 2010 at 10:11 am
Just found the site, love the post and can’t wait to flip through many more articles. Thanks a million.
January 10, 2010 at 2:58 pm
I’ve been looking for a way to show my tweets on google maps while I travel the US. That way friends and family can see where I currently am. Can I do that using this same method with a few tweaks? Any info would be wonderful.
Thanks.
January 11, 2010 at 4:26 am
Hi Arthur,
twitter’s api give you the possibility to do a lot of special and refined searches and with a little work you can modify our map in order to obtain what you want.
Essentially I suppose that you will need to tune the query params in our sample code and use some system to post geo-located tweets (we did not cover in the post creation of tweets because it would require authentication that was out of our aim).
Actually as ‘JooinK-team’ we are working on something that can be used to obtain your desired result so …
let’s keep in touch. :-)
January 13, 2010 at 12:42 pm
We modified the sample app to get some information on Haiti from Twitter http://www.jooink.com/snippets/mapHaiti/
February 6, 2010 at 8:01 pm
Does anyone know why this map is not allowing markers to be clicked in Safari and Chrome?
February 7, 2010 at 2:48 am
Hi Jeff,
the app is far from being robust and actually it is a sample code, but as I can see it works with Chrome (4.0.249.49 beta) and with Safari (4.0.4) .
Please give me details about the problem.
Francesca
February 7, 2010 at 1:14 pm
Sorry! My mistake — the map markers actually fire perfectly in Safari 4.0.4 and Chrome — I was using a dev version of webkit and that’s where saw the problem, but there’s not much anyone could do about that.
Great code, I’m really enjoying playing with it, thanks for the excellent write up!