Monday, July 18, 2011

Accessing the Google Public Location Badge programmatically

Introduction

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.

http://www.google.com/latitude/apps/badge/api?user=3356651955207924992&type=json

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": "http://www.google.com/latitude/apps/badge/api?type=photo&photo=gJsAQTEBAAA.rGdvZsJpNd7LFS7MT5g_Bg.YtjvkPGuqXl4iqhY7QGOyg",
"photoWidth": 96,
"photoHeight": 96,
"placardUrl": "http://www.google.com/latitude/apps/badge/api?type=photo_placard&photo=gJsAQTEBAAA.rGdvZsJpNd7LFS7MT5g_Bg.YtjvkPGuqXl4iqhY7QGOyg&moving=true&stale=true&lod=4&format=png",
"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 = "http://www.google.com/latitude/apps/badge/api";

 public static HttpRequestFactory createRequestFactory(final HttpTransport transport) {
     
    return transport.createRequestFactory(new HttpRequestInitializer() {
     public void initialize(HttpRequest request) {
      GoogleHeaders headers = new GoogleHeaders();
      headers.setApplicationName("Google-Latitue-Public-Badge-Sample");
      request.headers=headers;
      JsonHttpParser parser = new JsonHttpParser();
      parser.jsonFactory = new JacksonFactory();
      request.addParser(parser);
     }
  });
 } 

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 {

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

  public static class Geometry {
   @Key
   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");
  //System.out.println(request.execute().parseAsString());
  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 :


  4.0.0
  
    com.google
    google
    5
  
  com.ecs.latitude
  publicbadge
  1.0-SNAPSHOT
  
    
      
        maven-compiler-plugin
        
          1.6
          1.6
        
      
    
  
  
   
      com.google.api.client
      google-api-client
      1.4.1-beta
    
   
      com.google.api.client
      google-api-client-googleapis
      1.4.1-beta
        
  
  
   
  google-api-services
  http://mavenrepo.google-api-java-client.googlecode.com/hg
 
  
  
    UTF-8
  



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

Latitude 4.47658
Longitude 51.02511

References