Archive for the ‘mashup’ Tag
A Twitter – Google Maps mashup to view your friends network …
Filed under: social networking | Tags: google, maps, mashup, twitter
Comments (1) So it all started off with me wanting to see the geographical distribution of my Twitter friends (the Twitter-ers that I am following) and since I don’t host a website (and WordPress doesn’t allow JavaScript in blog posts) I had to do all this in Java.
In the end it worked out pretty well – it was actually quite simple using the Google Static Maps API. All you need to know is some scripting language and you need a Google Maps API Key.
So you start off with the Twitter REST API and get all your friends (the people that you are following) . I used a Java URL object but you can use cURL or any other scripting language that has HTTP URL support. The output looks something like this (the Twitter ids of all your friends) :
<?xml version="1.0" encoding="UTF-8"?> <ids> <id>14112660</id> <id>798731</id> <id>15256245</id> <id>16824408</id> <id>17191554</id> <id>18743188</id> <id>26459315</id> <id>21276419</id> <id>18702371</id> <id>34632001</id> <id>14576471</id> <id>13954772</id> <id>16038405</id> <id>15880443</id> </ids>
You have to now parse out the IDs from the above output and for each ID (and for yourself also), you have to once again use the Twitter API to get each user’s information. Now the output looks something like this :
<?xml version="1.0" encoding="UTF-8"?>
<user>
<id>12683672</id>
<name>Kartick Suriamoorthy</name>
<screen_name>karticks</screen_name>
<location>Munich, Germany</location>
<description>Software professional (JAVA, Network Management, Open Source, Application Servers, etc.)</description>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/57456567/got-beer_normal.png</profile_image_url>
<url>https://karticks.wordpress.com</url>
<protected>false</protected>
<followers_count>29</followers_count>
<profile_background_color>9ae4e8</profile_background_color>
<profile_text_color>000000</profile_text_color>
<profile_link_color>0000ff</profile_link_color>
<profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color>
<profile_sidebar_border_color>87bc44</profile_sidebar_border_color>
<friends_count>14</friends_count>
<created_at>Fri Jan 25 14:36:13 +0000 2008</created_at>
<favourites_count>0</favourites_count>
<utc_offset>3600</utc_offset>
<time_zone>Berlin</time_zone>
<profile_background_image_url>http://static.twitter.com/images/themes/theme1/bg.gif</profile_background_image_url>
<profile_background_tile>false</profile_background_tile>
<statuses_count>109</statuses_count>
<notifications></notifications>
<following></following>
<status>
<created_at>Mon May 25 05:25:11 +0000 2009</created_at>
<id>1909795818</id>
<text>End of a long weekend (and a long vacation). Now trudging back to stuttgart with my morning kaffee and breze.</text>
<source><a href="http://help.twitter.com/index.php?pg=kb.page&id=75">txt</a></source>
<truncated>false</truncated>
<in_reply_to_status_id></in_reply_to_status_id>
<in_reply_to_user_id></in_reply_to_user_id>
<favorited>false</favorited>
<in_reply_to_screen_name></in_reply_to_screen_name>
</status>
</user>
Now you have to parse out the location (4th element) and use the Google Geocode API to geocode the location. This is a little bit tricky – as sometimes the location attribute can be empty (you have to skip those users) or they can be iPhone lat-long coordinates (i.e. they are already geocoded – see below)
<location>iPhone: 30.417343,-97.710770</location> <description>I do not fear you, gypsy. I only want your tears.</description>
Once you have all the geocodes, you construct a Google Static Maps URL by using your geocode as the center, and all the geocodes (including your location) as markers. I used a black marker to mark my location, and red markers to mark my friends. The result looks something like this …
Ok not very impressive, but still fun …
Caveats :
– Google Static Maps API allows only 50 markers, so if your friends list is more than 50, you will have to prune out the unwanted ones 😉
– The Google Geocode API returns coordinates of a point as a longitude-latitude pair but the markers in the Google Static Maps API expect it in latitude-longitude format and it took me some time to figure out this (I was wondering why my markers were located in Antarctica and sometimes way outside any conceivable point in the globe !!). I wonder why Google did that (iPhone actually got it right and it was trivial to take the iPhone Geocode and insert it into the Google Maps URL) – it would have been just so easy to pass the lat-long pair from the geocode output into the Google Maps URL (to construct the marker’s coordinates) but now I had to add parsing logic to swap the lat-long locations (ugly).
Anyway, I hope this was fun and enjoyable, and as always, the code (TwitterGoogleMashup.java) can be found in the Google Code project, DalalStreet (in the FunStuff project). You can run this class with your own Twitter username (and Google Maps API key) by checking out the project into Eclipse. It will print out a URL when it finishes running and all you need to do is copy and paste this URL into your browser.
Until next time …
