27.10.2014 Views

Google Maps API 3

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 2 ■ TRANSFERRING FROM VERSION 2 TO 3<br />

Figure 2-3. In v3 it’s possible to have several InfoWindow objects open at the same time, something that was<br />

impossible in v2.<br />

In some ways, InfoWindow objects were easier to use in v2, but in v3 they are more powerful and<br />

conform better to how the other overlay objects work.<br />

Here’s an example of how to tie an InfoWindow to a marker and open it on the marker’s click event.<br />

In v2 you can just call the openInfoWindowHtml() method of the Marker object. In v3 you need to first<br />

create an InfoWindow object and then open it using its open() method.<br />

Version 2<br />

// Add a click event to a marker that will open an InfoWindow<br />

GEvent.addListener(marker, 'click', function() {<br />

marker.openInfoWindowHtml('Some text');<br />

});<br />

Version 3<br />

// Create a new InfoWindow object<br />

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

content: 'Some text'<br />

});<br />

// Add a click event to a marker<br />

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

// Add the InfoWindow to the map<br />

infoWindow.open(map, marker);<br />

});<br />

15

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

Saved successfully!

Ooh no, something went wrong!