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....



Monday, September 13, 2010

Pass parameters to Struts forward

You can add  parameters in the code execute(..) method in Action class, before the request is forwarded desired location as declared in struts-config.xml.
The mapping in struts-config.xml file is like this...

<action path="/TheAction" type="com.Actions.TestAction">
    <forward name="success" path="/View.jsp"/>
    </action>

And then in the execute(..) if we want to add parameter called "id" to the request it can do like this...

 public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

StringBuffer path=new StringBuffer((mapping.findForward("success")).getPath());
        String id="123"
        path.append("?ID="+id);
        return new ActionForward(path.toString());


}
 

Introduction

I am sampathpremarathna and i am a software Engineer .This is about my experience as a software developer.In here it will be post technical problems with solutions that i found for it.