我正在尝试形成对Twitter REST API的查询。有没有办法将gps坐标传递给查询并返回位于该位置特定半径内的推文。
答案 0 :(得分:1)
使用GET search/tweets端点,您可以指定地理编码和半径,以从该区域内获取一组推文。参数值如下:纬度,经度,半径。
Ex:37.781157,-122.398720,1mi
答案 1 :(得分: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);
}
答案 2 :(得分: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);
}
}}