Liferay 6.0, OpenSocial and Gadgets - Part 2

This is the second part of a series of blogs and wiki articles on using gadgets in Liferay. In the blog we'll look at a gadget in action and in the associated wiki post we'll breakdown the gadget, as well as have downloadable source code for you to try out

In the first post in the series, we talked about how to deploy a gadget. This time we'll build a gadget that will call a web service.

The web service we are using is one at GeoNames (www.geonames.org/export/ajax-postalcode-autocomplete.html)

It is called with a postal code and country and returns a list of matching locations. It uses a simple HTTP GET to call the web service and gets JSON (Javascript Object Notation) as the result. You'll find that a web service that returns JSON is much easier to work with to parse the returned data and display to the user. Unfortunately, not all web services have JSON output as an option. In a future entry we'll look at a SOAP web service and parsing the XML returned from it. But for now, we'll keep it simpler.

The gadget looks like this on the page. It uses some external javascript from GeoNames to determine your country based on the your IP address. I'm using this to tell the user where we think they are.

gadget

If we fill in the data with a zip code of 91789 and press the Get Location we'll see the following. Normally for the US we''ll only get one result as that's how postal codes are set up. But for other countries we may get multiple results. 

Note that by default it will return up to 10 results. But we may need more. So the gadget has a configuration to set the max rows to return. We can set this higher if we need to get more results. The maxrows setting is part of your gadget definition and automatically becomes a configuration option in Liferay through the OpenSocial portlet. No programming necessary!

So after configuring it to return up to 25 rows, we see that there are more than 10 results.

The web service returns more items than we are using, so if you download the gadget and read about the GeoNames web service, you can add more items to display to the user (such as latitude and longitude).

I hope the blog and wiki article and source are helpful for you. Thanks for reading!