27.10.2014 Views

Google Maps API 3

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

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

CHAPTER 7 ■ INFOWINDOW TIPS AND TRICKS<br />

Providing the HTML As a String<br />

In this example, you will create an InfoWindow that will contain an image a heading, some text, and a link.<br />

The image you will use is of a squirrel with the dimensions 100 × 100 pixels. It’s included in the code that<br />

comes with the book. The final HTML will look like this:<br />

<br />

<br />

<strong>Maps</strong> are awesome<br />

Some sample text<br />

A sample link<br />

<br />

Notice that the HTML elements are enclosed inside . You’re doing this because you<br />

want to be able to pinpoint this content from the style sheet.<br />

The end result will look like Figure 7-2.<br />

Figure 7-2. An InfoWindow with styled HTML in it<br />

Before you create the content, you will have to make sure that you have a InfoWindow object. You will<br />

use the same approach as described in Chapter 5 where you define a global variable called infoWindow<br />

that you reuse for each window that is opened. Each time the user clicks a marker, you check whether<br />

the variable infoWindow contains an InfoWindow object. If it doesn’t contain one, you create it. You’ve<br />

already defined the infoWindow variable at the top of the code. You now need to check whether it exists:<br />

// Check to see if an InfoWindow already exists<br />

if (!infoWindow) {<br />

infoWindow = new google.maps.InfoWindow();<br />

}<br />

134

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

Saved successfully!

Ooh no, something went wrong!