[1 of 3] Updating your brightkite location using Google latitude

BrightKite is a service, much like twitter actually, but that uses your location to find interesting stuff. It could be a great tool to create ad-hoc gatherings of some group of people, for instance coders. If there were a lot of BrightKite users in Bergen for instance, I could leave a message on brightkite that I and some other Python coders were going for a beer at Henriks, and that any others that is interested could join if they wanted, together with the GPS coordinates for Henriks. Other BK users in Bergen interested in Python could then see that message based on the proximity to that location and maybe the search term BrightKite.

The problem is that, though there are BK applications for the iPhone, theres no client for the S60 mobile market, and also the iPhone app provides a poor precision on the location.

I came to think of the Google Latitude service, which is a service that retreives your GPS location, and makes it available online, and to other Google Maps users in your Google contacts list when you’re using the Google Maps application. (though this is a privacy concern). I started investigating, and discovered that they have a “badge” you can use to post on your website.

This post is going to tell you how to use the data provided by this service in your Python apps. Upcoming posts will tell you how to turn this data into an address, using the Google Maps API, and the final post will show you how to use this address to update your BrightKite location through their API.

I strongly discourage using this badge as it is, as it exposes your private URL’s for your location information, and once these are available on another page than the google latitude badge page, you can never use the the webservices I’m about to tell you about, without possibly having strangers get to it.

As long as you keep these URL’s private, you should be pretty safe.

Go to the Google Latitude Badge page, and enable the service at best available precision, then go to the bottom of the page, where you would find a public json feed. It’s url looks someting like this: http://www.google.com/latitude/apps/badge/api?user=YOUR_SECRET_ID&type=json We’re going to use that URL in a moment.

The script below defines a GLatitude class, which we will initialize with your secret id (the long number from the json feed url), and which we then can use to get the latitude and longitude of the current location the Latitude service has on you.

Note that you can specify your location yourself using the privacy settings in the Google Maps application, so you can perfectly well lie about your location.

?View Code PYTHON
import urllib2, json
class GLatitude:
  def __init__(self,uid):
    self.uid=uid
  def getLocationData(self):
    return json.load(urllib2.urlopen("http://www.google.com/latitude/apps/badge/api?user=%s&type=json" % self.uid))
  def getCoords(self):
    return self.getLocationData()['features'][]['geometry']['coordinates']

It’s really simple. All I do is to fetch the json data, and lets the json module parse it. then the getCoords() method extracts the coordinates from the data returned.

I placed this code in a file called GoogleMaps.py, which we also will use in the next post, so I can use it as a module later on.

To use this then, you could do the following:

?View Code PYTHON
import GoogleMaps
gl=GoogleMaps.GLatitude("YOUR_SECRET_ID")
myLocation=gl.getCoords()
print "I'm located at latitude %s and longitude %s " % myLocation

Running this would produce something like the following:

I’m located at latitude 60.390218 and longitude 5.330515

Stay tuned for the rest of this tutorial.