javatwittergeolocationtwitter4jgeotagging

Collecting geotagged tweets with Twitter4j


Hi All,

   public static void main(String[] args) {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey("xxxxxx");
    cb.setOAuthConsumerSecret("xxxx");
    cb.setOAuthAccessToken("xxxx");
    b.setOAuthAccessTokenSecret("xxxxxxx");

    TwitterStream twitterStream = new    TwitterStreamFactory(cb.build()).getInstance();

    StatusListener listener = new StatusListener() {

        @Override
        public void onException(Exception arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

        }

        @Override
            public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatus(Status status) {
            User user = status.getUser();

            // gets Username
            String username = status.getUser().getScreenName();
            System.out.println(username);
            String profileLocation = user.getLocation();
            System.out.println(profileLocation);
            long tweetId = status.getId(); 
            System.out.println(tweetId);
            String content = status.getText();
            System.out.println(content +"\n");
            GeoLocation geolocation = status.getGeoLocation();

            System.out.println(geolocation +"\n");


        }

        @Override
        public void onTrackLimitationNotice(int arg0) {
            // TODO Auto-generated method stub
            System.out.println("onTrackLimitationNotice" +"\n");

        }

        @Override
        public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub
            System.out.println("onStallWarning" +"\n");

        }

    };
    FilterQuery fq = new FilterQuery();
    double lat = 53.186288;
    double longitude = -8.043709;
    double lat1 = lat - 4;
    double longitude1 = longitude - 8;
    double lat2 = lat + 4;
    double longitude2 = longitude + 8;
    twitterStream.addListener(listener);
    double[][] bb= {{lat1,longitude1}, {lat2 ,longitude2}};

    // fq.track(keywords);
    fq.locations(bb);


    twitterStream.filter(fq);  

}

This code is to collect tweets in the general UK & Ireland location but doesn't collect tweets or sometimes (rarely) collects tweets from areas outside the bounding box.

If i widen the bounding box I do get tweets but again sometimes they are outside the bounding box.

I am working from the logic that the 1st point of the bounding box is SW corner and next point is NE corner.

Any ideas what could be the problem? I am using Twitter4j 3.0.3

Thanks,

David


Solution

  • I think you are sending the coordinates in the wrong order. According to Filter Spec, Twitter expects a comma-separated list of longitude,latitude, but you are sending (latitude,longitude) pairs.

    The correct version should be:

    double lat = 53.186288;
    double longitude = -8.043709;
    double lat1 = lat - 4;
    double longitude1 = longitude - 8;
    double lat2 = lat + 4;
    double longitude2 = longitude + 8;
    
    double[][] bb = {{longitude1, lat1}, {longitude2, lat2}};
    
    FilterQuery fq = new FilterQuery();
    fq.locations(bb);
    twitterStream.filter(fq);