Close and Go BackBack to Viget

Google Maps the jQuery Plugin Way

Brian Landau
Brian Landau, Web Developer, July 17, 2009 5

One of the most common feature requests from our clients is Google Map integration. On two projects recently I found myself creating very similar functionality: a map of points with links in a nearby list that activate the markers on the map. This seemed like an excellent opportunity to package up functionality into some sort of “module.” Given that jQuery is my framework of choice I decided to go the route of a jQuery plugin (see David’s recent post for the advantages of jQuery plugins).

My goal when crafting this plugin was to use the convention-over-configuration paradigm, yet allowing customization, and to have the data be embedded in the HTML, allowing for graceful degradation in the rare cases where Google Maps wouldn’t be compatible. This would also allow the javascript the end user would need to write to be very minimal; one line is the default case.

The requirements for the most recent project were simple enough:

  1. Create a Google Map in a specific div.
  2. Add markers for given locations.
  3. Color the markers based on a category they are assigned to.
  4. Center the map around the markers and set it to a reasonable zoom level.
  5. When a link on the page for the location is clicked it should activate the marker and load the map info window with specific HTML.

Basic Usage

The most basic usage would be with HTML like this:

<div id="map"></div>

<div id="map-side-bar">
  <div class="map-location" data="{id: 2, point: {lng: -122.4391131, lat: 37.7729943}, category: 'restaurant'}">
    <a href="#" class="map-link">Nopalito</a>
    <div class="info-box">
      <p>The best authentic Mexican restaurant in San Francisco.</p>
    </div>
  </div>
  <div class="map-location" data="{id: 3, point: {lng: -122.4481651, lat: 37.8042096}, category: 'museum'}">
    <a href="#" class="map-link">Exploratorium</a>
    <div class="info-box">
      <p>A hands-on museum of science, art, and human perception in San Francisco.</p>
    </div>
  </div>
</div>

And Javascript like this:

$(document).ready(function(){
  $('#map').jMapping();
});

Working with options

The one thing this code doesn’t take care of is how to color the markers based on the categories. Coloring markers can easily be done with the Google Maps utility library MapIconMaker. To get it functional we just change the Javascript to this:

$(document).ready(function(){
  $('#map').jMapping({
    category_icon_options: {
      'restaurant': {primaryColor: '#E8413A', cornerColor: '#EBEBEB'},
      'museum': {primaryColor: '#465AE0', cornerColor: '#EBEBEB'},
      'default': {primaryColor: '#7CDF65'}
    }
  });
});

There’s a whole host of other options you can use to customize it. Most importantly you can set the selectors the plugin uses to find the locations, links, and the info box elements. There are even more examples and explanations available at its home on GitHub. That’s also where you can find the downloads, report issues, or fork the code.

In the future, I hope to add the ability to update the map, and other functionality that people request, as well as create some Rails helpers for integrating it into your views.

Look for more articles down the line here about how the plugin was designed and structured, and the way it’s tested.

James Wragg said on 07/19 at 07:54 AM

Your basically embedding Geo data in the HTML, so why not use the more flexible & supported Geo Microformat, then you don’t have to rely on the HTML5-like data attribute.

I made something very similar here: http://www.exhalebrighton.co.uk/classes/ although I didn’t go as far as to package it up in a nice plugin & don’t support the grouping by category, which is a nice feature btw, maybe this information can be moved to into the title of a wrapping element?

Brian Landau said on 07/20 at 11:43 AM

My reasoning not to use the Geo Microformat is that the purpose of that format is to semantically encode Geographic information, and on all of the sites I’ve worked with they have no need to present the end user with the latitude and longitude data. Admittedly that information could be hidden via CSS or Javascript, but I believe information that you don’t ever intend to present to the user should not be marked-up in that fashion.

My choice to use the “data” attribute (which is not actually HTML5 compatible, only the data-* attributes are) is simply personal preference. The plugin will actually support storing the data any place that is supported by the jQuery Metadata plugin, including HTML5 data attributes.

Additionally, using the “data” attribute is supported by all the browsers, the only disadvantage is that it is not valid (X)HTML.

So using the “title” attribute as you suggested is something that you can very easily do by configuring the “metadata_options” setting.

James Wragg said on 07/20 at 12:45 PM

Your missing how the geo Microformat can be implemented. When using geo with the http://austin.adactio.com/.

So to add geo to a placename, your markup would be like this:
[url="http://microformats.org/blog/tag/google/"bbr title="37.8042096;-122.4481651" class="geo">Exploratorium</abbr>

Simple! And when viewing the page you’ll see the Operator Microformat Firefox plugin light up, it’ll also be <a ]understood by Google & Yahoo[/url] & and you don’t have to invalidate your HTML!

Apart from how the data’s stored, I’m digging the plugin - thanks for sharing! :)

James Wragg said on 07/20 at 12:47 PM

Wow, formatting screwed up there.. let me try again:

Your missing how the geo Microformat can be implemented. When using geo with the <abbr> element you can put the lat/lon in the abbr title attribute and the human readable place name in the contents (see the markup of the venues featured on the classes page I linked to) this method can also be seen on <a href="http://austin.adactio.com/">http://austin.adactio.com/</a>.

So to add geo to a placename, your markup would be like this:
<abbr title="37.8042096;-122.4481651" class="geo">Exploratorium</abbr>

Simple! And when viewing the page you&#x27;ll see the Operator Microformat Firefox plugin light up, it&#x27;ll also be <a href="http://microformats.org/blog/tag/google/">understood by Google & Yahoo</a> & and you don&#x27;t have to invalidate your HTML!

Apart from how the data&#x27;s stored, I&#x27;m digging the plugin - thanks for sharing! :)

bob said on 08/07 at 12:13 PM

i posted this to your other viget blog that discusses this, but you should look into EE’s ability to dynamically produce xml files on request.  then you just use jquery to slice up that xml and throw it where it’s needed.  it really seems like you’re taking the long way around on this one.

Commenting is not available in this weblog entry.

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

Your post has made me think!

We people get used to that what we daily do. And normally we forget that we have to evolve.

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


How many minutes in an hour?

Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.