编程语言
首页 > 编程语言> > java – 使用Twitter4j收集地理标记的推文

java – 使用Twitter4j收集地理标记的推文

作者:互联网

大家好,

   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);  

}

此代码用于收集英国和英国的推文.爱尔兰的位置,但不收集推文或有时(很少)从边界框外的区域收集推文.

如果我扩大了边界框我会收到推文,但有时他们会在边界框之外.

我的工作原理是边界框的第一个点是SW角,下一个点是NE角.

任何想法可能是什么问题?我使用的是Twitter4j 3.0.3

谢谢,

大卫

解决方法:

我想你是以错误的顺序发送坐标.根据Filter Spec,Twitter期望以逗号分隔的经度,纬度列表,但您正在发送(纬度,经度)对.

正确的版本应该是:

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);  

标签:java,twitter,geolocation,twitter4j,geotagging
来源: https://codeday.me/bug/20190629/1326640.html