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 4 ■ TAKING THE MAP FURTHER WITH MAPOPTIONS<br />

This will provide you with a page that looks something like Figure 4-15.<br />

Figure 4-15. The example map<br />

The page has two buttons with the IDs getValues and changeValues. Right now these buttons do<br />

absolutely nothing when they’re clicked, but you’re going to use their IDs to attach click events to them.<br />

By using the JavaScript method document.getElementById(), you create a reference to them. After you’ve<br />

pinpointed the buttons, you’re going to add a little something extra, a click event with the help of the<br />

onclick event listener. Let’s start with the getValues button. Insert this code right after the creation of<br />

the map:<br />

document.getElementById('getValues').onclick = function() {<br />

// put code here<br />

}<br />

An anonymous function is attached to the onclick event, and it’s inside this function that you will<br />

put the code to retrieve values from the map.<br />

The first value that you will get is the zoom level of the map. To do this, you’re going to use the<br />

getZoom() method. To display the value that this method returns, you’re going to use an alert, and you’re<br />

also going to add some text that explains what this value is:<br />

document.getElementById('getValues').onclick = function() {<br />

alert('Current Zoom level is ' + map.getZoom());<br />

}<br />

If you run the page now and click the getValue button, the alert shown in Figure 4-16 will trigger.<br />

66

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

Saved successfully!

Ooh no, something went wrong!