views:

207

answers:

0

I am attempting to get a Google Maps cluster working with ym4r_gm for Rails. I know the general code works because every point is being displayed on the map. The problem is that every point is being displayed on the map. I only have like 350 points (right now) so its not too bad without clustering, but I would like to have it working so that I can scale.

Relevant controller code:


  def bizmap
    @map = GMap.new( "map" )
    @map.control_init( :large_map => true, :map_type => true )

    @map.center_zoom_init( [ 40.228,-100.283 ], 1 )

    markers = Array.new
    Business.find(:all, :include => [:addresses], :conditions => ["addresses.website_only != 1"]).each do |biz|
      if (!biz.addresses.first.latitude.blank? and !biz.addresses.first.longitude.blank?)
        markers.push(
          GMarker.new(
            [biz.addresses.first.latitude,biz.addresses.first.longitude],
            :title => biz.name,
            :info_window => pretty_info_window( biz.name, biz.addresses.first )
          )
        )
      end
    end
    clusterer = Clusterer.new(markers, :min_markers_per_cluster => 2)

    @map.overlay_init(clusterer)    
  end

Relevant view code (had trouble getting the markup to look right, but trust me, the opening <'s are there in the real code):


    div id="aroundMeMapBox">
  %= GMap.header %>
  %= javascript_include_tag 'clusterer' %>
  %= @map.to_html %>
  %= @map.div(:width => 600, :height => 400) %>
    /div>

The relevant code generated by the view is here:


script src="/javascripts/clusterer.js?1248062398" type="text/javascript">
script type="text/javascript">
var map;
window.onload = addCodeToFunction(window.onload,function() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(40.228,-100.283),1);map.addOverlay(new Clusterer([addDescriptionToMarker(addInfoWindowToMarker(new GMarker(new GLatLng(34.086,-83.8644),{title : "Georgia Biz"}),"Georgia Biz
101 Main St.
Buford, Georgia 30519",{}),"Georgia Biz") ... (removed for brevity) map.addControl(new GMapTypeControl()); } }); /script>

There is very little documentation on how to do clustering with Ym4r. I will gladly write a HOWTO once I figure this out.