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 2 ■ TRANSFERRING FROM VERSION 2 TO 3<br />

Marker Icons<br />

In v2, to change the default icon of a marker, you had to create a GIcon object and assign it to the marker<br />

by using the icon property of the GMarkerOptions object. Alternatively, you could use the setImage()<br />

method of the Marker object and pass a URL to an image as its parameter.<br />

In v3 you have a few more options. You can set the icon directly using the icon property of the<br />

MarkerOptions object, or you can set it later using the setIcon() method of the Marker object. In<br />

both cases, you can choose whether to use a full-fledged MarkerImage object or simply to use a URL<br />

to an image.<br />

In v2 the GIcon object included everything about the marker icon, such as its shadow, its printImage,<br />

and so on. In v3 this is handled differently. For example, the icon shadow is handled as a separate<br />

property in the MarkerOptions object. It’s called shadow and also takes either a MarkerImage object or a<br />

URL to an image as its value.<br />

All the alternative icons you could define in v2, such as printImage, mozPrintImage, and transparent<br />

are dropped, so you only need to worry about providing one image for the icon and one for its shadow.<br />

In its simplest form, changing the marker icon requires that you only provide it with a URL for the<br />

icon and one for the shadow. Well, you could actually omit the shadow property if you like. When<br />

changing the default icon, the default shadow is also removed.<br />

Version 2<br />

// Create a custom icon<br />

var myIcon = new GIcon(G_DEFAULT_ICON, 'icon.png');<br />

// Create a marker and add it to the map<br />

var marker = new GMarker(new GLatLng(54, 12), {<br />

icon: myIcon<br />

});<br />

map.addOverlay(marker);<br />

Version 3<br />

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

position: new google.maps.LatLng(54, 12),<br />

map: map,<br />

icon: 'icon.png',<br />

shadow: 'shadow.png'<br />

});<br />

In v2 the GIcon object has a property called imageMap. It’s used to define the clickable part of the icon<br />

and takes an array of integers as its value. In v3 this is defined by using the shape property of the<br />

MarkerOptions object, which takes a MarkerShape object as its value. This object has two properties, type<br />

and coord. The type property defines the kind of shape you would like to use, such as a polygon, circle, or<br />

rectangle. The coord property takes an array of integers defining the points in the shape. It works just like<br />

an ordinary HTML ImageMap.<br />

Version 2<br />

// Create a custom icon<br />

var myIcon = new GIcon(G_DEFAULT_ICON, 'icon.png');<br />

myIcon.imageMap = [4,4, 29,4, 29,29, 22,29, 17,35, 16,35, 10,29, 4,29, 4,4]<br />

13

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

Saved successfully!

Ooh no, something went wrong!