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 7 ■ INFOWINDOW TIPS AND TRICKS<br />

That’s the three required properties that you always have to set. In this case, however, you’ll want to<br />

set one more property. Since this will be a very small map, I don’t want it cluttered by the zoom and pan<br />

control or by any other control. To get rid of those, you set the disableDefaultUI property to true. This<br />

way, there will be no controls obscuring the view on the map.<br />

var overviewOpts = {<br />

zoom: 14,<br />

center: marker.getPosition(),<br />

mapTypeId: map.getMapTypeId(),<br />

disableDefaultUI: true<br />

};<br />

Now you’re all set to initialize the detail map. You have a container for it, and you have defined its<br />

properties. All that’s left to do is to create it.<br />

var detailMap = new google.maps.Map(detailDiv, overviewOpts);<br />

Now the map is created, but you also want a marker in it marking the spot.<br />

First you set the position. Now, you can do this in one of two ways. You could get it from your newly<br />

created detail map or from the clicked marker. In this example, I’ve chosen to get it from the latter.<br />

Next you need to define what map the marker should be added to. In this case, it’s the newly created<br />

detail map.<br />

That’s it for the required properties, but I’m going to add one more property. Since you don’t want<br />

this marker to be clickable, you want to somehow disable that. You can do this by setting the property<br />

clickable to false. This way, the cursor won’t change when the user hovers with the mouse over it, and<br />

the user can’t interact with it.<br />

OK, you’re set to create the marker. Here’s the code to do that:<br />

var detailMarker = new google.maps.Marker({<br />

position: marker.getPosition(),<br />

map: detailMap,<br />

clickable: false<br />

});<br />

That’s it for the map. It has been created, and the marker has been added to it. But you still<br />

can’t see anything if you try to click the marker. That’s because a crucial part is still missing, creating<br />

the InfoWindow.<br />

Creating the InfoWindow<br />

First you check to see whether you already have an InfoWindow. If not, you create one.<br />

if (!infoWindow) {<br />

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

}<br />

Next you’re going to set its content and then add it to the map. You probably know how to do this by<br />

now, but I’ll explain it anyway. First you’ll set the content by using its setContent() method. Then you<br />

call its open() method and pass a map object and a marker to it. The only thing you have to keep in mind<br />

is to pass it the right map and the right marker. In this case, it’s the big map and the clicked marker.<br />

148

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

Saved successfully!

Ooh no, something went wrong!