How'd they do that?

August 25, 2008

Have you ever gone to a site, looked at the page and wondered, "how did they do that?" Since we launched our 2008 U.S. Election site I am getting lots of questions about that nifty little "page element" sitting in the center of the page. Everyone wants to know, "how did you guys do that? how did you pack so much valuable and tightly scoped information in such a small amount of space?" Take a look at the page element below, play around with it a little bit and when you are ready, continue reading.



If you know how to use Firebug, then you can see that behind each tab (Election News, YouTube, etc.), there is a simple AJAX APIs JSON-P request. For some tabs the base operation is a Google Web Search or Custom Search, for others a Google News Search, and for others, a simple YouTube Search. The AJAX APIs work really well in this mode where the page author determines both the query and search options, as well controlling the end-to-end UI and resulting user experience.

But back to the tabs, and to answer the questions, "How'd they do that?", I will walk through each tab and show you the AJAX API requests that are used to generate the results for this page element:

First, the Election News tab. For this tab, we simply target News Search with the name of the candidate and a query addition of unitedstates_uselections. Something like this:

http://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=Barack%20Obama%20unitedstates_uselections

Next, the YouTube tab. For this tab, we use the Video Search system to directly target each candidate's YouTube Channel (e.g., ytchannel:barackobamadotcom), render the results with a simple fade in effect, and then bind each result click to a dynamically created player. Very simple stuff. Check out the url we use to grab the results:

http://ajax.googleapis.com/ajax/services/search/video?v=1.0&q=ytchannel:barackobamadotcom

Next, the Blog Posts tab. For this tab, we built a little Custom Search Engine, populating it with several of the top political blogs. The search then becomes the name of the candidate plus a &cx= argument to indicate the Custom Search Engine to use for the search:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Barack%20Obama&cx=010222979794876194725:pqldevwuapa

See what I mean? Take the queries above, slap on a little UI, and any application can take Google News, Custom Search, Web Search, YouTube, etc. and create a very tight little customized search experience...

The Quotes tab is very cool. Instead of just searching for News articles, this tab searches for news articles that contain quotes spoken by one of the candidates. Again, a simple News search with a &qsid= argument to scope to quotes for a particular candidate and we have what we need:

http://ajax.googleapis.com/ajax/services/search/news?v=1.0&qsid=tPjE5CDNzMicmMa

Finally, the News by State tab. This one has two things going on. First there is the state selector control. We use the just launched google.loader.ClientLocation property to determine the user's current location. Then, we simply use this location to form a geo scoped query for the candidate's name in the politics category, scoped with a geo restrict using &geo=. Something like this:

http://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=Barack%20Obama%20any_politics&geo=CA,US

So there you have it. Take the above collection of page author defined queries, mix in a few hours of coding, throw in some product managers, a handful of lawyers, a few PR folks, and you have a clever little page element that delivers tons of very focused information.

To learn more about the APIs used in this page element, visit the AJAX Search API and AJAX Feed API documentation. To learn more about Custom Search Engines, check out the recently updated developer guide. To discuss the APIs with us and the community, visit us on our developer forum.

p.s. - If you really like that page element and have to have it on your site, you can iframe it in, or just include our JavaScript directly:


<iframe
title="In the News"
style="border: 0px none ; margin-left: 1em; width: 540px; height: 530px;"
src="http://www.google.com/uds/gadgets/inthenews/iframe.html"
frameborder="0">
</iframe>


Or if you prefer including the raw JavaScript controls where you can make mild UI modifications with simple CSS rules, try this approach:


<style>
@import url("http://www.google.com/uds/gadgets/inthenews/inthenews.css");
body {
font-family: "arial", sans-serif;
font-size: 11px;
}
</style>

<script src="http://www.google.com/uds/gadgets/inthenews/inthenews.js"></script>
<script src="http://www.google.com/jsapi/" type="text/javascript"></script>

<!--[if IE]>
<script type="text/javascript"
src="http://www.google.com/uds/gadgets/inthenews/excanvas.js"></script>
<![endif]-->

<script type="text/javascript">
google.load("search", "1");
google.setOnLoadCallback(initialize, true);

function initialize() {
var options = {
format : "513x500",
useNav : true
};
new InTheNews("itnId-513x500", options);
}
</script>
</head>

<body>
<div id="itnId-513x500"></div>
</body>

Where is my current user?

August 21, 2008

Have you ever written an app and wanted to do something simple like center a map on your user's location? Consider a simple little map gadget. Wouldn't it be nice to be able to center that map on your user's home country, state, or city, instead of just arbitrarily centering on San Francisco or Australia?

Take a look at our new "API". Actually, it's not really an API, more like a simple object property called google.loader.ClientLocation.

After loading any one of our AJAX APIs, the system will attempt to geo-locate the client based on the client's IP address. If we have a valid mapping for the IP address, the google.loader.ClientLocation property is populated with coarse grained coordinates, the user's country, region, and city. The mapping isn't perfect, but it's pretty good, and is definitely usable as a way of setting smart defaults or adding a small amount of location awareness to your app.

Here is a little code sample that demonstrates the use of google.loader.ClientLocation:


/**
* Set the currentState_ and currentCountry_ properties based on the client's
* current location (when available and in the US), or to the defaults.
*/
InTheNews.prototype.setDefaultLocation_ = function() {
this.currentState_ = this.options_.startingState;
if (google.loader.ClientLocation &&
google.loader.ClientLocation.address.country_code == "US" &&
google.loader.ClientLocation.address.region) {

// geo locate was successful and user is in the states. range check
// the region so that we can safely use it when selecting a state
// level polygon overlay
var state = google.loader.ClientLocation.address.region.toUpperCase();
if (InTheNews.stateNames[[]state]) {
this.currentState_ = state;
}
}
this.currentCountry_ = "US";
}


To witness this code in action, check out our 2008 U.S. Election page. If you click on the "News by State" tab, you will either see political news, for the current candidate, scoped to your state, OR if we don't have a mapping for your IP address, you will see the same thing only scoped to the state of California.

Now for the good stuff... If you like this simple IP to location mechanism, wait until you see what the Gears Team announced. They have a killer version of this functionality that is able to pinpoint a user's location using IP, cell-ID, GPS, and coming soon, WiFi. This is definitely a step up in terms of both functionality and accuracy.

As always, feel free to leave us feedback, ask questions, or vote for some new features by visiting our developer forum.

Using the AJAX Language API while Travelling

August 12, 2008

Back in March we introduced the Google AJAX Language API, which you can use to incorporate translations from Google Translate into your projects. Lots of people have been excited about this API, including some engineers here at Google. Last Friday we released an iPhone interface for Google Translate built on top of the AJAX Language API. This interface exposes much of the functionality for languages translation in a simple UI for people to use on the go. To learn more, take look at this post on the Google Mobile blog.

AJAX Libraries API Update

August 5, 2008

What's better than making a drag/drop effect in Javascript that's compatible in all browsers? Not making a drag/drop effect in Javascript that's compatible in all browsers. Save your ninja-code for another day and use the JQuery UI plugin that is now hosted on our AJAX Libraries API! As usual, your users will get a performance boost when you use JQuery UI through our API, so get started by checking out the docs.

For those of you still waiting for a MooTools 1.2 update, we apologize for the delay. In fact, we had been waiting to release JQuery UI so we could push it with MooTools 1.2. What happened was that when MooTools released the 1.2 update, it had 1.2dev in the version string. We contacted MooTools and they immediately fixed this. However, Valerio Proietti over on their side has asked us to wait until 1.2.1 is out the door to flip the switch.