我正在尝试向Twitter REST API形成查询。有没有办法将GPS坐标传递到查询中,并返回位于该位置特定半径内的推文。Twitter - 查询特定地理位置半径内的推文
0
A
回答
0
试试下面这段代码。在尝试之前,您必须将一些值写入keyword
,latitude
,longitude
和radius
。 keyword
是您搜索的内容,我认为您可以理解其他值的含义。
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);
}
1
使用GET search/tweets端点,您可以指定地理编码和半径以从该区域内获取一组Tweets。参数值如下:纬度,经度,半径。
例:37.781157,-122.398720,1mi
0
基于@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);
}
}}
相关问题
- 1. 推荐一个web服务来处理特定半径内的位置?
- 2. 我想只显示特定半径的地理位置内的标记?
- 3. 地理位置API并在半径范围内查找用户
- 4. Mongodb公里半径的查询位置?
- 5. Google地理位置和半径
- 6. 安卓半径环绕地理位置
- 7. 纬度和长地理半径查询
- 8. html5地理位置在半径范围内搜索
- 9. 位置在一个地理位置科多瓦应用程序内的半径
- 10. 推文的Twitter API位置
- 11. 在Android中查找给定半径内的附近位置
- 12. Rails谷歌地图检查点是否位于特定半径范围内
- 13. HTML5地理位置 - 检测特定区域内的位置
- 14. 使用Twitter API确定推文位置
- 15. 获取半径范围内的位置
- 16. 从谷歌地图api v3中删除地理位置半径
- 17. 查询具有半径的场地
- 18. Twitter地理位置/搜索
- 19. 基于地理半径对推文进行聚类的算法
- 20. 了解推文上的地理位置
- 21. 特定半径内的邮编
- 22. 使用Google地图和推文的地理位置
- 23. HTML5地理位置中经度和纬度的半径
- 24. 使用PHP和MySQL的地理位置半径搜索
- 25. 检索给定位置的半径?
- 26. 特定圆半径
- 27. 当前的位置是在一个特定的半径Android
- 28. 查询与SQL,地理坐标和半径重叠的区域
- 29. 推特在Twitter Bootstrap 2.0定位?
- 30. 如何搜索特定半径的位置?
是否有使用Twitter4J Query做到这一点? – user3461851