brianjlandau (owner)

Revisions

gist: 139608 Download_button fork
public
Public Clone URL: git://gist.github.com/139608.git
Embed All Files: show embed
custom_mapping.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
if (GMap2){
  GMap2.prototype.centerAndZoomOnBounds = function(bounds) {
    this.setCenter(bounds.getCenter(), this.getBoundsZoomLevel(bounds));
  };
}
(function($){
  $.mapping = function(places, info_html_template, location_link_template){
    $.mapping.location_link_template = $.template(location_link_template);
    $.mapping.info_html_template = $.template(info_html_template);
    
    if (GBrowserIsCompatible()) {
      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.setMapType(G_NORMAL_MAP);
      map.addControl(new GSmallMapControl());
      map.centerAndZoomOnBounds($.mapping.getBounds(places));
 
      // Add the markers
      $.each(places, function(){
        var marker = $.mapping.createMarker(this);
        map.addOverlay(marker);
      });
 
      // Attach the click events to the links in the sidebar.
      $('a.map_item').live('click', function(e){
        e.preventDefault();
        var marker_index = parseInt($(this).attr('href').split('#')[1], 10);
        GEvent.trigger($.mapping.gmarkers[marker_index], "click");
      });
    }
  };
  
  $.extend($.mapping, {
    gmarkers: [],
    location_link_template: '',
    info_html_template: '',
    makeGLatLng: function(place){
      return new GLatLng(place.point.lat, place.point.lng);
    },
    getBounds: function(places){
      var bounds = new GLatLngBounds($.mapping.makeGLatLng(places[0]), $.mapping.makeGLatLng(places[0]));
      for (var i=1, len = places.length ; i<len; i++) {
        bounds.extend($.mapping.makeGLatLng(places[i]));
      }
      return bounds;
    },
    chooseIconOptions: function(category){
      switch(category){
        case 'communities-of-color':
          return {primaryColor: '#FF00FF'};
          break;
        case 'cross-population':
          return {primaryColor: '#33FFFF'};
          break;
        case 'women':
          return {primaryColor: '#0000CC'};
          break;
        case 'young-people':
          return {primaryColor: '#CCCCCC'};
          break;
        default:
          return {primaryColor: '#CC0000'};
          break;
      }
    },
    createMarker: function(place){
      var point = $.mapping.makeGLatLng(place);
      var custom_icon = MapIconMaker.createMarkerIcon($.mapping.chooseIconOptions(place.category));
      var marker = new GMarker(point, {icon: custom_icon});
      
      // bind the info HTML to the marker
      var info_html = $.mapping.info_html_template.apply({
        title: place.name,
        history: place.history,
        focus: place.focus,
        url: place.url
      });
      marker.bindInfoWindowHtml(info_html, {maxWidth: 425});
      $.mapping.gmarkers.push(marker);
      
      // add a line to the side_bar html
      $('#side_bar').append($.mapping.location_link_template, {
        name: place.name, id: ($.mapping.gmarkers.length-1),
        history: place.history, focus: place.focus,
        url: place.url
      });
      
      return marker;
    }
  };
})(jQuery);
 
places.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$(document).ready(function(){
  var myPlaces = [
    { point: {lat: 43.65654, lng: -79.90138},
      name: "This Place", category: 'blah',
      history: "Some History" },
    { point: {lat: 43.91892, lng: -78.89231},
      name: "Some Other Place", category: 'bogus',
      history: "More History"},
    { point: {lat: 43.82589, lng: -78.89231},
      name: "Another Place", category: 'something',
      history: "Weird History"}
  ];
  
  $.mapping(myPlaces, "${title}<br />History: ${history}", '<a href="#${id}" class="map_item">${name}</a><br />');
});