14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dangers of jQuery <strong>and</strong> Other Libraries Using Their Own Syntax<br />

Chapter 11 ■ Using Third-Party <strong>JavaScript</strong><br />

It is amazing how many day-to-day web-application <strong>and</strong> web-development tasks you can perform easily <strong>and</strong> quickly<br />

<strong>with</strong> jQuery. However, if you h<strong>and</strong>ed this document over to a third-party developer for maintenance, she’d have to<br />

know about jQuery or she would be completely lost. This is one of the dangers of using libraries. Instead of relying<br />

on <strong>JavaScript</strong> syntax <strong>and</strong> rules, you add an extra layer of necessary knowledge to the process. It is up to you to decide<br />

whether the benefits the library offers are worth that.<br />

Libraries are there to make the development process quicker <strong>and</strong> easier, not make us dependent on them or<br />

repeat mistakes we’ve made <strong>with</strong> libraries in the past already—creating applications <strong>and</strong> web sites that don’t work<br />

<strong>with</strong>out <strong>JavaScript</strong>.<br />

Next we’ll look at using the Google Maps API to create mapping applications.<br />

Using an API: Adding a Map to Your Site <strong>with</strong> Google Maps<br />

Google Maps (http://maps.google.com) is probably the web application that got the whole <strong>Ajax</strong> craze rolling.<br />

It provides the user <strong>with</strong> maps that can be moved around, zoomed, <strong>and</strong>—the implementation permitting—even<br />

annotated. You can show the location you want to see as maps, as satellite pictures, or as a mixture of both.<br />

Google allows web developers to use Google Maps on their own web sites by means of an API. To use the API,<br />

you need to sign up for a free developer key at its homepage:https://developers.google.com/maps/. Here you will<br />

also find the documentation <strong>and</strong> many examples of how to use Google Maps. The key will enable you to use maps on<br />

a single domain or subfolder in this domain. The examples in this chapter use a key that works <strong>with</strong> localhost, which<br />

means you need to run them in your local server via http://localhost/ <strong>and</strong> not from the file system.<br />

Once you have obtained the developer key, you can link to the <strong>JavaScript</strong> that contains all the map’s code in the<br />

head of your document. The “your key” shown in bold should be replaced <strong>with</strong> the key you obtained from Google:<br />

<br />

<br />

■ ■Caution This URL might change in the future, so be sure to check the API home page if your examples fail to work<br />

out all of a sudden.<br />

The next step you need to take is to get the latitude <strong>and</strong> longitude values of the location you want to display.<br />

If you type the location you are interested in into Google <strong>and</strong> then add the word “latitude” (for example “Saitama<br />

Japan latitude”), you can get the information you need to add to your map code.<br />

When you have the information, you can start adding your own map to your web site. As an example for an<br />

international location, let’s use Saitama Prefecture in Japan. The coordinates are 35.8617° latitude <strong>and</strong> 139.6453°<br />

longitude. Using this information <strong>and</strong> the methods provided for you by the API, it is easy to show a map of a nice place<br />

near Tokyo.<br />

You start <strong>with</strong> an HTML element to contain the map. You can add content in this element that is displayed when<br />

<strong>JavaScript</strong> is not available or when the browser isn’t supported. This content could be any HTML, text, or even a static<br />

image of the same map. The static image is a nice option to ensure backward compatibility; you just need to be sure<br />

not to tell the user anywhere in the text that the map is dynamic, because it might not be.<br />

www.it-ebooks.info<br />

329

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!