Add tweets (TweetComposer) - latitude and longitude


#1

I use simple code

TweetComposer.Builder builder = new TweetComposer.Builder(this)
.text(“just setting up my Fabric.”)
.image(myImageUri);
builder.show();

Can I add geographical coordinates to a post?


#2

As far as I know, you can’t add coordinates to a tweet when you use the Tweet Composer, but you can use the update method of the StatusesService to do that.

You could do it somehow like this:

StatusesService statusesService = TwitterCore.getInstance().getApiClient(session).getStatusesService();

statusesService.update("status", null, false, latitude, longitude, null, true, false, null, new Callback<Tweet>() {
             @Override
             public void success(Result<Tweet> tweetResult) {

                    // tweet successfully sent, you can do something with the result
            }

            @Override
            public void failure(TwitterException e) {
                  // there was some error, let the user know
            }
   });

Note: the arguments of the update method are the following: status (String), in_reply_to_status_id (long), possibly_sensitive (boolean), latitude (double), longitude (double), place_id (String), display_coordinates (boolean), trim_user (boolean), media_ids (String).

If you want to upload pictures with your tweet, first you’ll have to upload the pictures to the Twitter server by using the upload method of MediaService and get the ID(s) of the picture(s) from the result.


#3

Thank you
Yes, I’ve seen this code. In this example, there is no possibility to add a picture.


#4

You can, if you upload it with MediaService first and then use the ID you get back. I know that it’s a bit more complicated then just attaching the file with TweetComposer, but if you want to post coordinates, I’m afraid you have no other choice :slight_smile: Anyways, I posted tweets with pictures and coordinates using MediaService and StatusesService and it worked actually quite well.

Here is some code which might help you:

TwitterApiClient twitterclient = TwitterCore.getInstance().getApiClient(session);       

File photo = new File(mCurrentPhotoPath);
TypedFile typedFile = new TypedFile("application/octet-stream", photo);

MediaService ms = twitterclient.getMediaService();

// uploading the picture
ms.upload(typedFile, null, null, new Callback<Media>() {

    // uploading succeeded, we get the picture's ID in the response
    @Override
    public void success(Result<Media> mediaResult) {

        StatusesService statusesService = twitterclient.getStatusesService();

       // tweeting with coordinates and the picture (using its ID)
       statusesService.update("status", null, false, latitude, longitude, null, true, false,` mediaResult.data.mediaIdString, new Callback<Tweet>() {

            @Override
            public void success(Result<Tweet> tweetResult) {

                // posting succeeded
            }

            @Override
            public void failure(TwitterException e) {

                Log.e(TAG, e.getMessage());
            }


        });

    }

     // media upload failed
    @Override
    public void failure(TwitterException e) {

        Log.e(TAG, e.getMessage());
    }
});

#5