Activate your free membership today | Log-in

Thursday, December 13th, 2007

Unobtrusively Mapping Microformats with jQuery

Category: Articles, jQuery, Mapping, Microformat

<p>Simon Willison just doesn't stop. This time he has added a way that details the technique of unobtrusively mapping microformats with jQuery.

Simon puts together jQuery, microformats, and the Google Maps API to grok hCard and show maps of any hCard data that is found, ending up with:

JAVASCRIPT:
  1.  
  2. jQuery(function() {
  3.         // First create a div to host the map
  4.         var themap = jQuery('<div id="themap"></div>').css({
  5.                 'width': '90%',
  6.                 'height': '400px'
  7.         }).insertBefore('ul.restaurants');
  8.  
  9.         // Now initialise the map
  10.         var mapstraction = new Mapstraction('themap','google');
  11.         mapstraction.addControls({
  12.                 zoom: 'large',
  13.                 map_type: true
  14.         });
  15.  
  16.         // Show map centred on Brighton
  17.         mapstraction.setCenterAndZoom(
  18.                 new LatLonPoint(50.82423734980143, -0.14007568359375),
  19.                 15 // Zoom level appropriate for Brighton city centre
  20.         );
  21.  
  22.         // Geocode each hcard and add a marker
  23.         jQuery('.vcard').each(function() {
  24.                 var hcard = jQuery(this);
  25.        
  26.                 var streetaddress = hcard.find('.street-address').text();
  27.                 var postcode = hcard.find('.postal-code').text();
  28.        
  29.                 var geocoder = new MapstractionGeocoder(function(result) {
  30.                         var marker = new Marker(result.point);
  31.                         marker.setInfoBubble(
  32.                                 '<div class="bubble">' + hcard.html() + '</div>'
  33.                         );
  34.                         mapstraction.addMarker(marker);
  35.                 }, 'google');   
  36.                 geocoder.geocode({'address': streetaddress + ', ' + postcode});
  37.         });
  38. });
  39.  

You see the finished code at work.

Related Content:

2 Comments »

Comments feed TrackBack URI

Cool. We have been doing this some weeks ago, almost the same as mentioned here. We dubbed our system MapWorks (still in early beta), you can see a demo over here:
http://scoutinginvlaardingen.nl which maps all scouting groups in the dutch city Vlaardingen.

I had this idea for a long time. Simply just to read out the vcard microformatted html from the page, and plot them in a gMap. Very web 2.0.

Comment by 42answers — December 13, 2007

Simon is the, “master of disaster” – I mean… “master of good code”. It just doesn’t have the same ring to it, does it?

Comment by Marc — December 13, 2007

Leave a comment

You must be logged in to post a comment.