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 6 ■ MARKER ICONS<br />

■ Note After a MarkerImage object has been created, it can’t be changed. Therefore, if you need to change it, you<br />

have to discard it and create a new one.<br />

Adding a Custom Icon to a Marker<br />

Although the MarkerImage object contains properties that let you tweak it in different ways, in its<br />

simplest form, you need to provide only the first property, url. Let’s call this variable recycle since it will<br />

have a recycle icon.<br />

var recycle = new google.maps.MarkerImage('img/recycle.png');<br />

What you get is a plain icon (Figure 6-1). It’s nothing fancy, but you have a custom image.<br />

Figure 6-1. A plain icon<br />

Adding a Shadow<br />

MarkerOptions has an additional property called shadow that works like icon. It also takes a MarkerImage<br />

object as its value. You will add a link to an image of a shadow to the url property. The image looks like<br />

Figure 6-2.<br />

Figure 6-2. The shadow<br />

It looks like it will fit nicely beneath the new marker icon. Let’s name this variable shadow and point<br />

it to the image of the shadow:<br />

var shadow = new google.maps.MarkerImage('img/shadow.png');<br />

Putting It Together<br />

Now that you’ve created objects for the marker and the shadow images, you can put it all together by<br />

using them as values for the icon and shadow properties of the Marker object. This will create a marker<br />

with a nice shadow that gives a 3D feel to the map (Figure 6-3).<br />

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

position: new google.maps.LatLng(40.756054, -73.986951),<br />

map: map,<br />

icon: recycle,<br />

shadow: shadow<br />

});<br />

103

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

Saved successfully!

Ooh no, something went wrong!