Adding Google Maps To Your Rails Applications

Adding Information Windows

Figure 4 is clear proof you're getting somewhere, showing users where to find the best pizza in town. However, which icon points to Ianazone's, and which to Belleria's? You can rectify this problem by modifying the GMarker calls to include additional information that will display in an information window should the user click on the marker. The following code will add pizzeria names and mailing addresses to each window:

Notice the use of <br /> tags in the information window message to separate each line. You're free to use any valid HTML markup you please to dress up the format.

Changing the Default Icon

The default red teardrop icon is okay for a standard map, but what if you wanted to spruce things up a bit? With a bit of additional programming and some patience, you can change the default icon to one more fitting of your website's purpose. For instance, Figure 6 displays a map using a custom pizza box icon.

Figure 6: Adding an information window to the map

Related Articles

As you can see below, the code is fairly straightforward. First, you need to define the custom icon by creating a GIcon object, and defining the icon and corresponding shadow icon location. To offset the shadow icon, you'll see I'm stretching it an additional five pixels wide to achieve the shadow's casting effect. Next, you need to define the icon anchor relative to the marker coordinates, and finally the offset location of the informational window that will appear when the icon is clicked. Finally, you give the icon a name, in this case, pizza_icon, and define it as a variable.

Any time you'd like to subsequently use the custom icon, just pass that icon name into the GMarker declaration, as is shown below.

You might have noted I mentioned patience is a requirement when creating custom icons. This is because you'll need to spend some time creating the icon unless you can find one with an appropriate usage policy, and then experiment with shadow opacity and offset until the desired affect is achieved. Once complete, though, your users will marvel at your attention to detail!

Where to From Here?

Hopefully, this introductory tutorial leaves your mind racing regarding new possibilities for your Rails applications! This is just a sampling of things to come, though; in future installments, you're going to stretch the capabilities of Rails and the Google Maps API to their limits, so check back regularly for the latest updates!