【问题标题】:Twitter - Query for tweets within a radius of a particular GeoLocationTwitter - 查询特定地理位置半径内的推文
【发布时间】:2014-03-30 23:46:37
【问题描述】:

我正在尝试对 Twitter REST API 进行查询。有没有办法将 gps 坐标传递到查询中并返回位于该位置特定半径内的推文。

【问题讨论】:

    标签: java twitter geolocation jersey twitter4j


    【解决方案1】:

    使用GET search/tweets 端点,您可以指定地理编码和半径以从该区域内获取一组推文。参数值如下:纬度、经度、半径。

    例如:37.781157,-122.398720,1mi

    【讨论】:

    • 是否有使用 Twitter4J 查询的方法?
    【解决方案2】:

    试试下面的代码。在尝试之前,您必须将一些值放入keywordlatitudelongituderadiuskeyword 是您搜索的内容,我认为您可以理解其他值的含义。

    try {
            Query query = new Query(keyword); //
    
            GeoLocation location = new GeoLocation(latitude, longitude);
            String unit = Query.KILOMETERS; // or Query.MILES;
            query.setGeoCode(location, radius, unit);
    
            QueryResult result; 
    
            do {
                result = twitter.search(query);
                List<Status> tweets = result.getTweets();
    
                for (Status tweet : tweets) {
                    System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                }
    
            } while ((query = result.nextQuery()) != null);
    
        } catch (TwitterException te) {
            System.out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }
    

    【讨论】:

      【解决方案3】:

      基于@Seohyun Choi 的回答,这是带有身份验证的完整 java 代码。 在编译此代码之前,请在路径中添加 twitter4j-core-3.0.5.jar

      import java.util.List;
      import twitter4j.GeoLocation;
      import twitter4j.Query;
      import twitter4j.QueryResult;
      import twitter4j.Status;
      import twitter4j.Twitter;
      import twitter4j.TwitterException;
      import twitter4j.TwitterFactory;
      import twitter4j.conf.ConfigurationBuilder;
      
      public class TweetsInParticularLocation {
          private static final String TWITTER_CONSUMER_KEY = "****";
          private static final String TWITTER_SECRET_KEY = "*****";
          private static final String TWITTER_ACCESS_TOKEN = "****";
          private static final String TWITTER_ACCESS_TOKEN_SECRET = "***";
      public static void main(String arg[])
      {
          ConfigurationBuilder cb = new ConfigurationBuilder();
          cb.setDebugEnabled(true)
              .setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
              .setOAuthConsumerSecret(TWITTER_SECRET_KEY)
              .setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
              .setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET);
          TwitterFactory tf = new TwitterFactory(cb.build());
          Twitter twitter = tf.getInstance();
      
          try {
              Query query = new Query("India"); //
      
              GeoLocation location = new GeoLocation(20.593684, 78.962880); //latitude, longitude
              String unit = Query.KILOMETERS; // or Query.MILES;
              query.setGeoCode(location, 1, unit); //location, radius, unit
      
              QueryResult result; 
      
              do {
                  result = twitter.search(query);
                  List<Status> tweets = result.getTweets();
      
                  for (Status tweet : tweets) {
                      System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
                  }
      
              } while ((query = result.nextQuery()) != null);
      
          } catch (TwitterException te) {
              System.out.println("Failed to search tweets: " + te.getMessage());
              System.exit(-1);
          }
      
      }}
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2014-11-02
      相关资源
      最近更新 更多