Here’s a demonstration of how to use the twitter4j library to post a tweet and set a location from where it was posted.
This example requires a twitter4j.twitter object (the twitter variable) to be preconfigured with authentication tokens. Replace the text Orlando, FL with a string describing the user’s location, and the GPS coordinates with your user’s coordinates.
StatusUpdate status = new StatusUpdate(tweet_text);
/**
* We'll add location information for this tweet.
* Here, we tell Twitter that this tweet was made from Orlando, FL.
* The GPS coordinates here are for Walt Disney's Hollywood Studios theme park.
*/
status.placeId("Orlando, FL");
GeoLocation geolocate = new GeoLocation(28.355269, -81.559390);
status.setLocation(geolocate);
status.setDisplayCoordinates(true);
//And finally, post this tweet.
try {
twitter.updateStatus(status);
}
catch (TwitterException e) {
System.err.println("Problem accessing Twitter. Exception message: " + e.getMessage());
}
Remember the appropriate import:
import twitter4j.*;