Google AJAX Search API

Tags: Google, AJAX, API

Google has released a new AJAX search API for implementation in web pages. The AJAX API allows for web, local, video and blog searches. Google still has their Web API where you can search web pages through their webservice, but I'm guessing this is going to be phased out now.

Google is now spearheading AJAX for all sorts of web need. You probably already know about Google Maps (if not have a look around this site), and the new AJAX search API seeks to integrate with this service to allow users to do local searches. Many map developers have been begging Google for a geocoding service, and it seems like this new service can provide both geocoding and display search result locations. I'm not sure that the latter actually qualifies as reverse geocoding, but it certainly makes it easy to plot your search results on the map - which probably also means that we will see ads on the maps soon. For now the local search seems to work best with U.S. locations, but this should be rectified soon.

Conspicuously the AJAX search API does not provide searches for news, images or other of their specialized searches.

The API can be used to draw an AJAX search box, but more interesting is the ability to search programatically through the different search classes: GwebSearch(), GlocalSearch(), GblogSearch() and GvideoSearch(). The results can then be displayed in a customized and integrated manner on the web page.

A simple search can be done like so:


var s;

function DoSearch(query) {

s = new GwebSearch();
s.setSearchCompleteCallback(this, WriteSearch);

function WriteSearch() {
if (s.results.length == 0) { alert("No results"); }
else { for (var i = 0; i < s.results.length; i++) { alert(s.results[ i ].title); } }

The search string is passed to the DoSearch function, which in turn queries Google. The cool thing to notice is that users don't have to wait while the function gets the results. A search complete handler can be specified and this function will be called when the results arrive. In the example I merely loop through the results and display a message box, but in a real life implementation the results would be sent to a page element to be displayed.

I have also made a more complex implementation of the API that uses the local search function (for the moment this only works in the U.S and Canada). The local search function can help you find the nearest Chinese restaurant or can geocode and address simply by typing it into the search box. See the demo here.

A big thank you to Google for sharing their innovations with everyone.

Latest Tweets