Search code examples
geolocationprocessingtwitter4j

Processing twitter4j query with only Geolocation


Ok. Each time we get only 100 tweets. I want to show them on a map based on the Geolocation. Is there any way I can get 100 tweets that all of them have Geolocation. Because now only 3-4 out of 100 has Geolocation.

I read some examples with FilterQuery but Processing throws me an error on it, as unknown.


Solution

  • Here is an working example. This was tested using twitter4j 4.0.2

    The interface StatusListener is where you can do whatever you want with your tweets Kind of... is there that they arrive

    Using stream API:

    import twitter4j.util.*;
    import twitter4j.*;
    import twitter4j.management.*;
    import twitter4j.api.*;
    import twitter4j.conf.*;
    import twitter4j.json.*;
    import twitter4j.auth.*;
    
    
    TwitterStream twitterStream;
    
    
    double[][] boundingBox= {
      {
        -180, -90
      }
      , {
        180, 90
      }
    }; /// whole world;
    
    void setup() {     
      size(100, 100);    
      background(0); 
      openTwitterStream();
    }  
    
    
    void draw() {     
      background(0);
    }  
    
    
    
    // Stream it
    void openTwitterStream() {  
    
      ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setOAuthConsumerKey(FILL_IN);
      cb.setOAuthConsumerSecret(FILL_IN);
      cb.setOAuthAccessToken(FILL_IN);
      cb.setOAuthAccessTokenSecret(FILL_IN);
    
    
      TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
      twitterStream.addListener(listener);
    
      FilterQuery filter = new FilterQuery();
      filter.locations(boundingBox);
    
    
      twitterStream.filter(filter);
    
      println("connected");
    } 
    
    
    // Implementing StatusListener interface
    StatusListener listener = new StatusListener() {
    
      //@Override
      public void onStatus(Status status) {
        GeoLocation loc = status.getGeoLocation();
        System.out.println("@" + status.getUser().getScreenName() + " - " + loc);
      }
    
      //@Override
      public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
      }
    
      //@Override
      public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
      }
    
      //@Override
      public void onScrubGeo(long userId, long upToStatusId) {
        System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
      }
    
      //@Override
      public void onStallWarning(StallWarning warning) {
        System.out.println("Got stall warning:" + warning);
      }
    
      //@Override
      public void onException(Exception ex) {
        ex.printStackTrace();
      }
    };