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

A Clever Way of Dealing with Lots of Different Marker Icons<br />

In some applications that use a lot of different marker icons on the map, managing these icons can be a<br />

problem. You have to define a new MarkerImage for each different marker type, and you will have a<br />

massive if clause to handle which MarkerImage to use in each particular case.<br />

One technique to make all of this more manageable is to store all your MarkerImage objects in<br />

an array.<br />

You previously learned that each item in an array has an index. In JavaScript it’s possible to use a<br />

more descriptive label than an index to mark each item. This is called an associative array. What it means<br />

is that instead of getting an array item by its index number, you can get it by its label.<br />

var fruit = [];<br />

fruit['apples'] = 20;<br />

fruit['oranges'] = 10;<br />

fruit['bananas'] = 15;<br />

To access the number of oranges, you simply call the array with the label oranges.<br />

fruit['oranges']; // returns 10<br />

You’ll utilize this to manage your MarkerImage objects. In this example, you’re going to plot weather<br />

onto a map. To do this, you’ll need several different icons that indicate what the weather is like (see<br />

Table 6-5). You’re just going to use three different weather types for the brevity of the example, but as<br />

you can imagine, if this were a real weather map, you would need a lot more.<br />

Table 6-5. Weather Icons<br />

Icon Weather Filename<br />

Cloudy weather<br />

clouds.png<br />

Rainy weather<br />

rain.png<br />

Sunny weather<br />

sun.png<br />

These are just a few of the icons available in the Tango Weather Icon pack, which is free for<br />

commercial use. You can download it from http://darkobra.deviantart.com/art/Tango-Weather-Icon-<br />

Pack-98024429.<br />

You start by defining the array that will contain the icons:<br />

var weatherIcons = [];<br />

Now that you have an array, you will start adding MarkerImage objects to it. Let’s start with the<br />

clouds icon:<br />

122

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

Saved successfully!

Ooh no, something went wrong!