02.06.2013 Views

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Adding Google Maps<br />

to Your Site<br />

latitude : 45,<br />

longitude : -122,<br />

zoom : 15<br />

}); // end goMap<br />

$.each(markers,function(i,marker) {<br />

$.goMap.createMarker(marker);<br />

}); // end each<br />

In addition, you might want to remove markers (for example, a button that says<br />

Hide Markers). GoMap includes a clearMarkers() function. You use it like the create-<br />

Marker() function; that is, you attach it to the <strong>jQuery</strong> object instead of a selection,<br />

like this:<br />

$.goMap.clearMarkers();<br />

So say you have a button on the page with the ID of removeMarkers. You can attach<br />

a click handler to it that will remove the markers from the page:<br />

$('#removeMarkers').click(function() {<br />

$.goMap.clearMarkers();<br />

}); // end click<br />

If you wish to remove just a particular marker, you need to first add an ID to a<br />

marker, then call GoMap’s removeMarker function. For example, the code below<br />

creates a new map (lines 1–13), and then adds a click event to a page element with<br />

the ID of remove.<br />

1 $('#map').goMap({<br />

2 markers : [<br />

3 {<br />

4 latitude : 45.53940,<br />

5 longitude : -122.59025,<br />

6 title : 'Marker 1',<br />

7 id : 'marker1'<br />

8 }<br />

9 ],<br />

10 zoom : 15,<br />

11 maptype : 'ROADMAP',<br />

12 scaleControl : true<br />

13 }); // end goMap<br />

14 $('#remove').click(function() {<br />

15 $.goMap.removeMarker('marker1');<br />

16 });<br />

<strong>The</strong> important part in creating the marker is line 7—id : ‘marker1’—which assigns<br />

an name to that marker. Once a marker has an ID, you can identify it and remove it<br />

using the removeMarker function (line 15). GoMap also provides a showHideMarker()<br />

function, which toggles a marker from visible to invisible and vice versa. For example,<br />

you could rewrite lines 14–16 so each time the page element is clicked, the<br />

particular marker is either hidden or shown, like this:<br />

$('#remove').click(function() {<br />

$.goMap.showHideMarker('marker1');<br />

});<br />

396 javascript & jquery: the missing manual<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!