Monday, July 18, 2011

Accessing the Google Public Location Badge programmatically


The Google Public Location Badge provides a convenient way to share your Latitude location with others. The Google Latitude apps page allows you to enable your Public Location Badge, and provides you with an HTML snippet that you can add to your blog or website, where it will render a Google Map.

JSON responses

Another way of accessing your location through the Public Location Badge is by using JSON. This allows for a more programmatic access of your location that you can embed in your applications.

This method is very handy if you want to access the current location of someone who has exposed his location through the Public badge.

Each public location badge is associated with a userid. This numeric value is associated with each google account.

If you want to access the JSON string, you can type the following URL in your browser.

It should return a string like this :

{"type": "FeatureCollection", "features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [4.47658, 51.02511]},
"properties": {
"id": "3356651955207924992",
"accuracyInMeters": 0,
"timeStamp": 1311050105,
"reverseGeocode": "Arrondissement of Mechelen, Belgium",
"photoUrl": "",
"photoWidth": 96,
"photoHeight": 96,
"placardUrl": "",
"placardWidth": 56,
"placardHeight": 59

Java access

We can use the Google APIs Client Library for Java to access your public location badge from a java application. It provides a simple an elegant way to make the REST call, and transform the JSON string into a java based model.

The sample application I'll be showing here is very simple, and is made up of 1 java class that contains a main method.

We start by creating our HttpTransport and requestfactory, required to perform the REST call.

private static final HttpTransport transport = new ApacheHttpTransport();
 private static final String PUBLIC_BADGE_URL = "";

 public static HttpRequestFactory createRequestFactory(final HttpTransport transport) {
    return transport.createRequestFactory(new HttpRequestInitializer() {
     public void initialize(HttpRequest request) {
      GoogleHeaders headers = new GoogleHeaders();
      JsonHttpParser parser = new JsonHttpParser();
      parser.jsonFactory = new JacksonFactory();

The Java model

We also need to model the JSON string into java. As we're interested in the users location, the simplest model I could come up with looks like this :

public static class PublicBadge {

  public Feature[] features;
  public String type;
  public static class Feature {
   public Geometry geometry;

  public static class Geometry {
   public float[] coordinates;


Performing the REST call

What's left now is to perform the actual call, and parse the response into our java model.

public static void main(String[] args) throws Exception {

  HttpRequestFactory httpRequestFactory = createRequestFactory(transport);
  HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PUBLIC_BADGE_URL));
  request.url.put("user", "INSERT USER ID HERE");
  request.url.put("type", "json");
  PublicBadge publicBadge = request.execute().parseAs(PublicBadge.class);
  System.out.println("Longitude " + publicBadge.features[0].geometry.coordinates[0]);
  System.out.println("Latitude " + publicBadge.features[0].geometry.coordinates[1]);

Project setup

You can copy paste these bits of code into a single java class. In order to use the
Google APIs Client Library for Java, you need to have the following dependencies in place :

The easiest way to do that is to define the Google APIs Client Library for Java dependencies in a pom.xml, and use maven to resolve the dependencies for you. The pom.xml to have this sample up and running looks like this :




The sample app will simply output the latitude / longitude associated with the Public Badge.

Latitude 4.47658
Longitude 51.02511


1 comment:

  1. I am no longer surprised on what Google can do, almost everything is amazing and very useful! Thank heaven that Google was made and made our life much easier.

    "April" for plan de travail cuisine