Loading...
Logo
Processing Forum
I've been exploring the Twitter4j library for a class demo, with Jer Thorp's tutorial as a starting point.
So far everything has worked well, but I notice that every time I try to grab a geo location, it's consistently NULL. 

Am I missing something? Or can it really be that SO MANY tweets have no geo-locations associated with them?? I've tried every method, object/constructor and field I can find with "geo" in the name... Everything has been null. 

For testing, this code just pulls one tweet using noLoop() and a getTweet() function. A mouse event gets a new tweet... 

//Build an ArrayList to hold all of the words that we get from the imported tweets
ArrayList<String> words = new ArrayList();
ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitter;

void setup() {
  //Set the size of the stage, and the background to black.
  size(100, 100); 
  background(0);
  smooth();

  //Credentials
  cb.setOAuthConsumerKey("yours");
  cb.setOAuthConsumerSecret(" yours ");
  cb.setOAuthAccessToken(" yours ");
  cb.setOAuthAccessTokenSecret(" yours ");
  //Make the twitter object and prepare the query
  twitter = new TwitterFactory(cb.build()).getInstance();
  noLoop();
}

void draw() {
  getTweet();
}

void getTweet()
{
  Query query = new Query("i%love%you");
  query.setRpp(1);

  //Try making the query request.
  try {
    QueryResult result = twitter.search(query);
    ArrayList tweets = (ArrayList) result.getTweets();
    Tweet t = (Tweet) tweets.get(0); //just get one for now...
    double lat=0;
    double lon=0; //These aren't doing anything right now

    String user = t.getFromUser();
    String msg = t.getText();
    Date d = t.getCreatedAt(); 

    //Attempted Geotagging
    GeoLocation g = t.getGeoLocation(); //Nothing...
    GeoQuery gq = new GeoQuery(g); //Nothing...
    GeoLocation ng = gq.getLocation();

    println(g + ":" + ng); //Nulls all around
    println(msg);

  }
  catch (TwitterException te) {
    println("Couldn't connect: " + te);
  }
}
void mousePressed()
{
  redraw(); // Get a new tweet
}



Replies(2)

Hi professomk, I'm struggling with the same issue. For the getLocation() function I found an answer here:  http://stackoverflow.com/questions/8150551/getlocation-returns-null-what-am-i-missing (last comment). You need to use the user class instead of the tweet one to make it work. But it doesn't seem to be the same with getGeoLocation(). I think that the number of geotagged tweets could be actually very low (being the service very demanding for batteries). 
Thanks for your input! I'll try it out.