Taking the geographical location of a tweet

May 5, 2012 at 1:16 PM

HI Joe,

I'm working on an application that searches the Twitter hashtags according to some, in which the geographic location of the tweet is extremely important.
I wonder if I have any alternative to get the location of tweets returned to me.

Coordinator
May 14, 2012 at 1:56 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
May 14, 2012 at 2:29 AM

Hi,

There was a bug with Status coordinates.  The fix has been checked into source code: http://linqtotwitter.codeplex.com/SourceControl/changeset/changes/7d2c457e2fe4.

This will also be included in the next release.

Joe

May 23, 2012 at 2:53 PM

Hi Joe,


I downloaded the new library and tested again Geo parameter, but it displays the following error: "Object reference not set to an instance of an object.".
I do not know there are errors in my implementation, or if a solution to this has beenimplemented. Follow the line on which to assign the values ​​of this parameter:

 

_tweets.LocalizacaoGeografica = tweet.Geo.Coordinates.SingleOrDefault().Longitude.ToString() + ":" + tweet.Geo.Coordinates.SingleOrDefault().Latitude.ToString();

 

Where "_tweets" is registered in a DB in the format string.

May 23, 2012 at 9:50 PM

Hi,

Actually, "Geo" is a soon to be deprecated and is considered obsolete.

So instead of using "tweet.Geo.Coordinates.SingleOrDefault().Longitude.ToString()"

           use "tweet.Coordinates.Longitude", and if you wanted to add the SingleOrDefault() and ToString() to your code you can(However you decide to use it.)

Here's a nice tool to use as well if you don't already have it. http://www.fiddler2.com/fiddler2/

Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet.

So basically you can watch your HTTP requests and drill down into the json format and see what you're pulling.(You'll be able to look to see if coordinates are being brought back at all)

Also beware that if a client doesn't have there locations turned on(GeoEnabled = true/false) then it will not return any coordinates or any location information for that matter.

So if you run some tests, make sure you have an account with the locations turned on(you can do that in the settings on your twitter account)

I hope that helped you out some  :-)

Definitely come back if you have any more questions.

Thank you,

Andre Mayo

 

May 24, 2012 at 2:49 PM

Here I am again to ask questions ...


I downloaded the latest version of the DLL and can not directly access the property "Coordinates" directly by the object "tweet", I must first pass through "Geo", follows thecomplete code of the query:

var result = (from tweet in twitterCtx.Search

where tweet.Type == SearchType.Search &&

tweet.PageSize == 100 &&

tweet.Page == page &&

tweet.Hashtag == hashtag[indexHashtag]

select tweet).SingleOrDefault();

 

foreach (var tweet in result.Results)

{

//other assignments

 _tweets.LocalizacaoGeografica = tweet.Geo.Coordinates.SingleOrDefault().Longitude.ToString() + ":" + tweet.Geo.Coordinates.SingleOrDefault().Latitude.ToString();

}

 

there are any errors there?

May 24, 2012 at 6:32 PM
Edited May 24, 2012 at 8:50 PM

Hi Savyo,

    I need to apologize to you. It was to my understanding that I thought you were using "twitterCtx.Status" for your query. In "Status" is where Geo is being deprecated.

But in twitterCtx.Search, Geo is still valid so don't worry about looking for the coordinates property through tweet. You can use tweet.Geo.Coordinates.

I see you are trying to make a query with "twitterCtx.Seach"

I know exactly how to fix your problem here :-)

Let's look at your query.

 

var result = (from tweet in twitterCtx.Search
              where tweet.Type == SearchType .Search &&
                    tweet.PageSize == 100 &&
                    tweet.Page == page &&
                    tweet.Hashtag == hashtag[indexHashtag]
             select tweet).SingleOrDefault();


 So this is your query right here. The reason you are not getting anything is because you didn't specify a query.
 So here is what you need to add to your query.
 tweet.Query == "george"(Your query can be anything, screenName, hashtag, userID, or just any keyword I put george because it brings back actual coordinates)

I would reccomend starting off with a simple query and then adding your original parameters.(Or whatever you choose)

var result = (from tweet in twitterCtx.Search
              where tweet.Type == SearchType .Search && 
                    tweet.Query == "George"
             select tweet).SingleOrDefault();     

Now for your foreach loop here's an example I've provided for you so you can display tweets with coordinates only(As we discussed earlier, some tweets just don't have coordinates.)

foreach(var tweet in results.Results)
{
    if(tweet.Geo.Coordinates.Count == 0)
    {
        Console.WriteLine("No Coordinates on this tweet");    else
    }
        Console.WriteLine("Longitude: {0}, Latitude: {1}",
    {
       
        tweet.Geo.Coordinates[0].Longitude, tweet.Geo.Coordinates[0].Latitude);
    }
}

Above me I made a foreach loop to iterate through the tweets we are querying, also I added an "if" statement to check the count of coordinates to see if they're any at all.

 If the coordinates Count is not 0 then we display those coordinates.

Inside of that "else" statement you can execute your code.

 

 

_tweets.LocalizacaoGeografica = tweet.Geo.Coordinates.SingleOrDefault().Longitude.ToString() 
+ ":" + tweet.Geo.Coordinates.SingleOrDefault().Latitude.ToString();

And I checked you code, and everything is good to go
Just make sure you add the query I gave you and query something you know has coordinates or something you put coordinates into yourself.

 Once again I hope this helps and ask me any questions if you're having problems from here :-)

Thanks,

Andre Mayo


 


 

Jun 5, 2012 at 1:33 PM

Hi Andre,

I made the implementation indicated by you, following the same steps, but the return is always null.

I tested the implementation with a tweet of mine that owned location, and the Count property of Geo.Coordinates returned 0 in my tweet that had geographic location.

This part of the API has been implemented? or it will still come in later versions?

I thank you for the information passed and the time spent to my questions.

Coordinator
Jun 7, 2012 at 3:01 PM

Are you using the latest version of LINQ to Twitter?  I just released 2.0.26.

Joe

Jul 12, 2012 at 2:26 PM

Hello again,

I come here once more to ask for information.I used the newest version of the DLL (2.0.27) and managed to get the geographic location of tweets (better late than never).But I need some information more generalized, more exactly the city where the tweet was made. Do you know any API (preferably in. NET) that gives the possibility of viewing a city from the latitude / longitude?
Thanks again for the information

Jul 12, 2012 at 3:09 PM
Edited Jul 12, 2012 at 3:10 PM

Hi Savyo,

Heres something that I believe will help you out.

https://dev.twitter.com/docs/api/1/get/geo/reverse_geocode

You can use geo/reverse_geocode and be able to pull the city/country inside of "Places" using longitude and lattitude.

I hope this helps.

 

Andre

Feb 27, 2014 at 5:32 PM
Edited Feb 27, 2014 at 5:34 PM
Hello,

I have the same question below but unfortunately the code does not because Twitter have changed the API (tweet.Geo does not work).

I use the version 3.0.2.12654 of LinqToTwitter (LinqToTwitterPCL).

Here my search Query to fiand all twitts that contains a keyword ("toto")
var srch =
            (from search in twitterCtx.Search
             where search.Type == SearchType.Search 
             && search.Query == "toto"                  
             select search).SingleOrDefault();
Until all is well.

I want to return the text, but also sender's latitude and longitude :

foreach (var twt in srch.Statuses)
        { 
           string res = String.Format(Longitude : {0}, Latitude : {1}, Twitt :  {2}",
                     twt.Coordinates.Longitude, 
                     twt.Coordinates.Latitude, 
                     twt.Text);
         Console.Writeline(res);
        }
The problem is all Longitude, latitude are 0. I made several tests with different keywords, but I still have the same result : 0 for longitude and 0 for latitude.
Did I miss something?

Thanks for your help.
Coordinator
Feb 27, 2014 at 8:42 PM
Hi cherkalid,

When Latitude and Longitude are zero, it means that the coordinates aren't available. A couple reasons might be that the user/user's software didn't provide the coordinates or the user has turned location tracking off for their Twitter account.

@JoeMayo
Feb 28, 2014 at 6:20 AM
Thank you JoeMayo for your quick response.
I found is abnormal even trying with the a keyword like "cocacola", I find nothing!
So, I thought I do not use the right property/class.
That said, I always look for a keyword that will allow me to find twittes with coordinates for the rest of my test (If you have an idea), namely the use of PowerMap for viewing, but that's another topic.

Thanks
Coordinator
Feb 28, 2014 at 3:35 PM