Sunday, October 24, 2010

Get location from google latitude api

This is about application that get the location of my google latitude friends and it is very interesting. Initially i did it with OAuth protocol but it was not succeed.Is was not easy than i though and it almost looked like it is impossible because every time i access google latitude app it asked me to logging to gmail and grant access to get my location from latitude but it is not we wanted .  Then i try it in following way and then i succeeded . For this first you have to get user id from the latitude application.
you can do it by this way
 https://www.google.com/latitude/apps -> Enable and show best available/Enable and show city-level only

Then you can get JSON or ATOM format user details from latitude and then you can get the location. I used JSON format and for that i wanted a JSON parser. I found a JSON parser form here.(JSON parser and other necessary utils )

then after integration of those you just want to call its like this
Location l =LatitudeJSONParser.getPosition("http://www.google.com/latitude/apps/badge/api?user=" + userid+ "&type=json"); 

The JSON parer returns a Location object and we can get latitude and longitude from that object.
String  latitude = l.getLatitude();
String  longitude = l.getLongitude();
String time=l.getTime();

After getting location you have to map the user with user id. And thats all we have to do....