确定。每次我们只收到100条推文。我想在地图上显示地理位置。有什么方法可以得到100条推文,所有这些推文都有Geolocation。因为现在只有3-4个人有地理定位。
我用FilterQuery阅读了一些例子,但是处理系统会向我抛出一个错误,如未知。
答案 0 :(得分:3)
这是一个工作示例。这是使用twitter4j 4.0.2进行测试的
接口StatusListener是你可以用你的推文做任何你想做的事......那是他们到达的
使用流API:
import twitter4j.util.*;
import twitter4j.*;
import twitter4j.management.*;
import twitter4j.api.*;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.auth.*;
TwitterStream twitterStream;
double[][] boundingBox= {
{
-180, -90
}
, {
180, 90
}
}; /// whole world;
void setup() {
size(100, 100);
background(0);
openTwitterStream();
}
void draw() {
background(0);
}
// Stream it
void openTwitterStream() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(FILL_IN);
cb.setOAuthConsumerSecret(FILL_IN);
cb.setOAuthAccessToken(FILL_IN);
cb.setOAuthAccessTokenSecret(FILL_IN);
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
twitterStream.addListener(listener);
FilterQuery filter = new FilterQuery();
filter.locations(boundingBox);
twitterStream.filter(filter);
println("connected");
}
// Implementing StatusListener interface
StatusListener listener = new StatusListener() {
//@Override
public void onStatus(Status status) {
GeoLocation loc = status.getGeoLocation();
System.out.println("@" + status.getUser().getScreenName() + " - " + loc);
}
//@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
}
//@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
}
//@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
}
//@Override
public void onStallWarning(StallWarning warning) {
System.out.println("Got stall warning:" + warning);
}
//@Override
public void onException(Exception ex) {
ex.printStackTrace();
}
};
答案 1 :(得分:0)
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
//import twitter4j.FilterQuery;
import java.util.*;
Twitter twitter;
String searchString = "shopping";
Query query;
ArrayList tweets,tweets2;
void setup()
{
frameRate(0.2);
tweets = new ArrayList();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("*");
cb.setOAuthConsumerSecret("*");
cb.setOAuthAccessToken("*");
cb.setOAuthAccessTokenSecret("*");
twitter = new TwitterFactory(cb.build()).getInstance();
query = new Query(searchString);
getNewTweets();
}
void draw()
{
try{
if (current >= tweets.size()) //if u read all the received tweets make a new query
{
tweets.clear();
tweets2.clear();
getNewTweets();
current = 0;
}
Status currentTweet = (Status) tweets.get(current);
GeoLocation loc = currentTweet.getGeoLocation();
User currentUser=(User) currentTweet.getUser(); //Get User info
double latitude = currentTweet.getGeoLocation().getLatitude();
double longitude = currentTweet.getGeoLocation().getLongitude();
println( "Longtitude: "+longitude);
println( "Latitude: "+latitude);
String user = currentUser.getScreenName();
String msg = currentTweet.getText();
println( "User: "+user);
println("Message: "+ msg);
}
catch (Exception te) {
println(te);
}
}
void getNewTweets()
{
try
{
// FilterQuery filtro = new FilterQuery();
/* filtro.locations(boundingBox);
twitter.addListener(listener);
twitter.filter(filtro);
*/
query.setCount(100); //sets the number of tweets to return per page
QueryResult result = twitter.search(query);
tweets2 = (ArrayList) result.getTweets();
for (int i = 0; i < tweets2.size(); i++) {
Status currentTweet = (Status) tweets2.get(i);
GeoLocation loc = currentTweet.getGeoLocation();
if(loc!=null){ //add to list only tweets with geo location
tweets.add(tweets2.get(i));
}
}
}
catch (TwitterException te)
{
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
答案 2 :(得分:0)
不是答案,而是简单的解决方法:
我知道大多数人在发推文时都没有启用GPS,而其他人则不愿意分享他们的位置!
但他们仍在分享他们的位置!猜猜怎么样?在他们的个人资料他们的家乡,他们的国家大多是可见的,它可以给你一个大致位置的推文来自哪里!您可以使用Rest API
查询用户的个人资料,从而查询他/她的位置 twitter.showUser(userScreenName).getLocation();
答案 3 :(得分:0)
我认为如果你在搜索之前设置了Geocode,那么大部分结果将具有Geolocation(最多90%)。但我不知道为什么不是整个结果与地理位置。也许它会有所帮助?
GeoLocation obj = new GeoLocation(35.8007019, -97.3383211);
query.setGeoCode(obj, 2000, Unit.valueOf("km"));
result = twitter.search(query);