Chapter 11. jQuery and APIs: Objects, Objects Everywhere

image with no caption

As talented a developer as you are, you can’t do it all alone... We’ve seen how we can include jQuery plug-ins, like jQuery UI or the tabs navigation to help boost our jQuery app, without much effort. To take our applications to the next level, apply some of the really cool tools out there on the Internet, and use information provided by the big hitters—like Google, Twitter, or Yahoo!—we need something...more. Those companies, and many others, provide APIs (application programming interfaces) to their services so you can include them in your site. In this chapter, we’ll look at some API basics and use a very common one: the Google Maps API.

Dr. Pattersby and Dr. Gimli want to add some more cool features to their website—think you’re up to the task?

From: Dr. Gimli []

Subject: Some more updates to our site

Hey guys,

Thank you so much for helping make our website more user-friendly and easier to collect data about sightings. We’ve had a substantial increase in traffic, so we’re really excited to look at all the data we’re collecting.

We’ve had several requests to make the information more accessible to the masses. Lots of people are interested in the sightings data, so we’d like to give them a way to see what we’ve collected.

Here’s what we need:

  1. We’d like to be able to select a single sighting and view the information associated with it. Along with the information about the cryptid, we’d like to see the latitude/longitude information appear on a Google map.

  2. When a point is displayed on the Google map, we’d like to be able to click it to see more information about the sighting.

  3. We’d like to be able to select the creature type from a list and display all the creatures associated with that type from our database. We’d also like to see all the creatures with that type on a Google map, so we can find some sighting hotspots to examine more closely. Can all these points be clickable, as well as the list of creatures, so users can see more information about each one?

Not too much to ask, right, since we already have all the information stored?

Looking forward to hearing from you!

--

Dr. Gimli and Dr. Pattersby

cryptozoologists.org

image with no caption

Joe: Are you talking about the Google map we need to build?

Frank: Yep. Pretty straightforward...

Jim: Straightforward? They want a whole Google map!

Frank: Yep.

Jim: Multiple points on the map—one for each of their cryptids—each being clickable for more information...

Frank: Yeah, I think I know how to do that.

Jim: And custom click on the list to interact with the points and show the “more information” pop ups on the map.

Frank: Yeah, uh, I’m not too sure how to do that.

Joe: No worries on that part. The Google Maps folk provide an API we can use to get the job done.

Jim: AP what?

Frank: API. It’s short for application programming interface. It’s how companies like Google, Netflix, and Yahoo! enable us to use some of their cool tools on our own sites.

Jim: That does sound pretty cool, but will it give us all the pop ups and whatever else we need to put on the map?

Joe: Well, it should. Maybe we should look at the Google Maps API to see how it works.

For any API you want to use, you can look up documentation and get sample code online. We grabbed this sample from http://code.google.com/apis/maps.

image with no caption

That’s Google’s code.

It’s not as bad as it looks. Let’s see what’s going on in the code in a little more detail.

Back in Chapter 6, we created our own JavaScript objects with properties and methods to store and use information as we saw fit. Many companies—like Google, Netflix, Microsoft, and Yahoo!—also create API objects to allow us to interact with their data. If you need a little reminder, feel free to hop back there to reacquaint yourself with objects and how they work.

image with no caption
image with no caption
image with no caption

Because it speeds everything up.

Like we saw back in Chapter 6, objects offer smarter storage. You use objects when you need to store multiple variables about a particular thing. An API is just a series of object constructors that allow you to create your own instances of other people’s objects. Then, once you have an instance, you can use all the properties and methods associated with those objects in your code!

The complexities of creating a mapping application for everyone to use are exactly why Google uses objects in its API. That way, it can create objects with many different methods and properties for all the different elements you’d need to build a map.

From the code on the previous page, you can see that the map object has properties of zoom, center, and mapTypeId—plus many more that we haven’t shown here. The map also has many methods, like setCenter.

Do this!

Create a new page called display_one.html and save it in your project folder for this chapter.

image with no caption

First, make a copy of all the files you have from the end of the previous chapter. We’ll use all the same files in this solution too, so we might as well pick up where we left off. All that code plus your new display_one.html file will give us two important new things:

A div with the ID of map_canvas.

The Google Maps API code, by adding <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>.

To include a Google map on that page, you’ll need to create a maps.js file and add a function to it that calls the API code to build a map on the page.

Getting JSON data with SQL and PHP

Chapter 9 showed how a SQL select statement could pull the information you wanted out of the database so that a PHP file could turn it into JSON and return it to our page, using Ajax.

You also learned how to use Ajax to get JSON-encoded information from a PHP file. For PHP to return JSON data, it was easy—the json_encode function, which accepted an array, gave back JSON-encoded data so that jQuery could interact with it.

image with no caption

For this chapter, we’ve written all of the PHP and SQL for you. As long as you’ve got the MySQL database from Chapter 10, you’re good to go! The rest of the SQL and PHP code are in the downloads for this chapter. Feel free to run them on your own server. You can download all the PHP and SQL in a single file from http://thinkjquery.com/chapter11/end/service.zip.

When it comes to putting points on a map, Google’s got it down. However, Google doesn’t call them points. It calls them markers. Markers are objects—just like everything else in the Google Maps API—and have their own methods and properties for interaction and manipulation.

image with no caption
image with no caption

Here’s what we need in order to nail this last request:

image with no caption

You’ve got us there.

We need to update our code so we can get information from the database when the drop-down list is changed, instead of just showing the creature type in an alert box.

Then we can mark that one off the list. But while we’re at it, we should be able knock off the third item on the list too. Roll up your sleeves—we’re about to dive in and put this all together.

We’re almost at the end here, and by now you’ve seen a whole variety of events that jQuery and JavaScript provide for making fun, interactive web apps. Since the Google Maps API is just JavaScript (albeit very well written and efficient JavaScript), it too can utilize the browser’s ability to listen for events and act accordingly.

And for the same reasons that jQuery added its own event listener creation functions, the folks on the Google Maps end of things have provided the capability to add event listeners through the API. This is because not all browsers deal with event listeners in the same way, so this ensures that the API can control how the listeners are added to the page. Let’s have a look at adding an event listener for the click event to create a Google Maps pop up (also called an InfoWindow):

image with no caption

In just a few short pages, you managed to put together a fully functional website, using code from several different languages—PHP, SQL, JavaScript, and jQuery—as well as pairing up the the Google Maps API with Ajax and JSON to display some pretty complex data. No small feat!

image with no caption
image with no caption

You’ve got Chapter 11 under your belt and now you’ve learned how to pull together jQuery with APIs (plus JavaScript, PHP, MySQL, Ajax, JSON, and more!).

image with no caption

We’re sad to see you leave, but now that you have all the skills you need to build your own cool jQuery-powered websites, we’re sure you’d rather be doing that than hanging around here. It’s been great showing you around the world of jQuery. Feel free to drop us a line or tell us about your cool new site at the Head First Labs: http://headfirstlabs.com.