How can I visualise tweets on a map in Processing?

229 views Asked by At

I'm trying to use the twitter API to search for a keyword and get the location of that particular tweet to then visualise onto a map.

I've successfully created my map using unfolding maps and tilemill, I'm just struggling with the twitter part. Using the twitter 4J library, I've tried the following code but I'm not able to get the location as coordinates. Also, once I've got the coordinates I dont know how to go about visualising them on my map.

import twitter4j.*;
import twitter4j.api.*;
import twitter4j.auth.*;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.management.*;
import twitter4j.util.*;
import twitter4j.util.function.*;



ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitterInstance;
Query queryForTwitter;

void setup() {
 cb.setOAuthConsumerKey("**********");
 cb.setOAuthConsumerSecret("*******");
  cb.setOAuthAccessToken("*********");
  cb.setOAuthAccessTokenSecret("*********");

  twitterInstance = new TwitterFactory( cb.build()).getInstance();

  queryForTwitter = new Query("#nature");

  size(640, 440);
  background(0);
  FetchAndDrawTweets();
}

void FetchAndDrawTweets() {
 try {
    QueryResult result = twitterInstance.search(queryForTwitter);
    ArrayList tweets = (ArrayList) result.getTweets();
    for (int i=0; i<tweets.size(); i++) {
      Status t = (Status) tweets.get(i);
      String user = (t.getUser()).getLocation();
      text(user + ":" , 20,15+i*15);
    }
  } 
  catch(TwitterException te) {
    println("Couldn't connect:" + te);
  }
}here

I'm quite new to processing, so please be patient with me as I don't even know if I'm going about this the right way.

Any help would be gratefully appreciated!!

1

There are 1 answers

1
Kevin Workman On

Nice work on separating your problem into smaller sub-problems. It's a good idea to separate the "get tweets and locations" step from the "show stuff on map" step. Good work.

As for getting the location of a tweet, there are two places you should look:

The Twitter4J JavaDocs: This is a list of every class, function, and variable available to you from the Twitter4J library. This should be your first stop. Do you see anything that looks useful in here? Specifically, the Status class has a getGeoLocation() function that looks pretty promising.

The Twitter API: this is the underlying JavaScript API that the Twitter4J library is built on. Check out this documentation for more details on what's going on underneath the Twitter4J library. Specifically, this page says the the geo object is deprecated and that you should use the coordinates field instead.

So the first thing I would try is the getGeoLocation() function. But note that not every tweet will have a location, since users can disable location tracking. Also note that if the underlying JavaScript library no longer provides the geo object, and if the Twitter4J library is using that to populate its getGeoLocation() function, then you won't be able to get at the location through Twitter4J. I haven't tested that at all though.