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 5 ■ X MARKS THE SPOT<br />

■ Note Actually, when you click something, three events happen. They are click, mousedown, and mouseup. You<br />

can choose which one of these you want to capture. What you should know is that mousedown happens first, right<br />

when you hold down the mouse button. The mouseup event happens when you release the depressed mouse<br />

button, and click happens after mouseup and mousedown both have occurred. Similarly, when you press a key on<br />

the keyboard, three events happen. These are besides keypress, keydown, and keyup. Of these, keydown happens<br />

first, then keyup, and lastly keypress.<br />

Listen for the Events<br />

What these events have in common is that you can catch them in your code and do stuff when they are<br />

triggered. To do this, you need to add listeners. A listener is connected to an object and a certain event.<br />

It just sits quietly and waits for the event to happen. When the event does happen, the listener pops<br />

into action and runs some code. In the <strong>Google</strong> <strong>Maps</strong> <strong>API</strong>, there’s a method for adding a listener that’s<br />

called google.maps.events.addListener(). It takes three arguments:<br />

• The object it’s attached to.<br />

• The event it should listen for.<br />

• A function that is executed when the event is triggered. This function is called an<br />

event handler.<br />

Adding a Click Event to the Marker<br />

To add a click event to your marker, you need to extend the code with an event listener (Listing 5-8).<br />

Listing 5-8. Adding an Event Listener<br />

// Creating an InfoWindow with the content text: "Hello World"<br />

var infowindow = new google.maps.InfoWindow({<br />

content: 'Hello world'<br />

});<br />

// Adding a click event to the marker<br />

google.maps.event.addListener(marker, 'click', function() {<br />

// Code to be run...<br />

});<br />

This code will attach a click event to the marker that will run some code when it’s being triggered.<br />

Now all you have left to do is to write some code that will open the InfoWindow.<br />

The InfoWindow object has a method called open() that will open the InfoWindow and make it visible<br />

on the map. The open() method takes two arguments. The first argument is a reference to the map<br />

object that it will be added to (in case you have more than one map on the page). The second argument<br />

is the object that the InfoWindow will attach itself to. In our case, you want to attach it to the marker<br />

being clicked. The reason you need to do this is that you want the InfoWindow to know where it should<br />

position itself on the map. If you provide it with an object, it will automatically position itself so that the<br />

tip of the stem of the speech bubble will point at the object. This argument is actually optional. You<br />

81

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

Saved successfully!

Ooh no, something went wrong!