Brandon
Brandon

Reputation: 31

Geocoding Tweets on a Google Map

Would really appreciate any help with my issue. I've been looking at this terrific tutorial by Marcin Dziewulski who integrated recent tweets and twitter user avatars onto a Google Map. Basically he places the users avatar where they last tweeted. I highly recommend checking the tutorial out here:

http://tympanus.net/codrops/2011/04/13/interactive-google-map/

It all seems to make sense except when it comes down to the actual geocoding of the user. The code works beautifully when the different tweeters are from different cities. However, let's say you wanted to track certain users in one city. They're all geocoded from the center of the city and you can only see one avatar at a time. If you refresh the page, another one will show up, in the exact same spot. This obviously isn't what I want. I assume the problems is in this bit of code:

var users = o.twitter.get(), arr = new Array;
        for (i in users){
            var user = users[i];
            $.getJSON('http://twitter.com/users/show/'+user+'.json?callback=?', function(data) {
                var img = data.profile_image_url,
                    screen_name = data.screen_name;
                geocoder.geocode({ address: data.location }, function(response, status){
                    if (status == google.maps.GeocoderStatus.OK) {
                        var x = response[0].geometry.location.lat(),
                            y = response[0].geometry.location.lng();
                        marker = new google.maps.Marker({
                            icon: img,
                            map: map,
                            title: screen_name,
                            position: new google.maps.LatLng(x, y)
                        });

I think something needs to change in the geocoder.geocode({ address: data.location } bit of code, but not 100% positive. I can't seem to find the solution in the Twitter API documentation either.

My question is basically this...can the code above be altered so that it gathers more precise lat and long. coordinates from where tweets are actually generated and then display them correctly at those locations? In other words, don't just put a tweet from NYC in the middle of NYC, but place the avatar at the EXACT location.

I've tested this with my own tweets, with the the location feature of twitter enabled. I just can't figure this out! Thanks again to anyone who can help!

Best,

Brandon

Upvotes: 3

Views: 5588

Answers (2)

Pete Mancini
Pete Mancini

Reputation: 575

Unless the tweet was made from a mobile device it's lat/long is going to be where the ISP is registered. If I tweet from my desktop my tweets emanate from the street in front of the capitol here in Nashville. If I use my phone and I allow the twitter app to use my lat/long then that would be different. Trust me, I could instantly use the lat/long data in analysis of twitter streams and I wish it were that simple.

What you could do is add a jitter function that adds random displacement along both the x and y coordinate field to displace the tweets lat/long. Or you could grid it out. If you know how many of them you are getting you can figure out easily the bounding area of your grid and do it like that.

Upvotes: 0

Erik Hinton
Erik Hinton

Reputation: 1946

The problem is that the vast majority of tweets are not geocoded but the coords are simply guessed from the user's location settings. If you wanted to display this in an interesting way, consider doing a uniform distribution of "center of city" latlongs. This is what census data map-makers often do when they don't have any more exact positioning data than say a district or a tract.

Upvotes: 2

Related Questions