Topic: [SOLVED]Creating google maps markers through instances.

Greetings and congratulations for this amazing forum.

I wasn't sure on where to post this, so I'm sorry in advance if it's in the wrong section.

I'm playing around with google maps and RoR and I've hit a stall here. There is a database that contains info about some gas stations, that info is stored in an instance and google map markers are created using that instance.

<% for station1 in @station1 -%>
var type=<%= station1.AREA%>;
        var marker1 = new GMarker(new GLatLng(<%= station1.LONG%>, <%= station1.LANG %>));
        markerGroups[type].push(marker1);
        map.addOverlay(marker1);
        var html<%= station1.ID %> ="<img src='www.something.com/something.jpg'" +
                 "width='128' height='128'/> <br/>" +
                 "Test test testing<br/>" +
                 " - <%= station1.ID %> -";
    //    marker1.openInfoWindowHtml(html<%= station1.ID %>);
        GEvent.addListener(marker1, "click", function() {
          marker1.openInfoWindowHtml(html<%= station1.ID %>);
   
        });
       


<% end -%>


The above generates markers for stations in one specific area by declaring conditions on my controller. You can see the instance in the controller bellow.

 @station1 = Station.find(:all, :conditions => ["AREA = ?", '1' ])

I'm also using another JS function to toggle the visibility of the markers of each region. This is the function:

var markerGroups = { "1": [], "2": []};
        function toggleGroup(type) {
              for (var i = 0; i < markerGroups[type].length; i++) {
                var marker = markerGroups[type][i];
                if (marker.isHidden()) {
                  marker.show();
                } else {
                  marker.hide();
                }
              }
            }

The problem that I'm facing is that I would have to create 20 different instances (the number of the areas) and have 20 different loops to create the markers for each area in order to toggle their visibility (var markerGroups = { "1": [], "2": []}; Should extend to 20). Now it's not that it's too much to do, but it definitely isn't the optimal solution. Can anyone think of a way to achieve this with a loop here and there? Perhaps one in the controller? I'm lost sad

I hope you can pretty much understand what I'm trying to do here, if not ask any clarifications you might need.

Cheers.


Solved:

I've changed my tables a bit and came up with the following solution:

[code ruby]   @area = Area.find(:all)
    @station = {}
    for area in @area
     @station[area.ID] = Station.find(:all, :conditions => ["AREA_ID = ?", area.ID ])
    end[/code]

And in the view:

[code ruby]<% for area in @area -%>
<% x = area.ID -%>
<% @station[x].each do |key,value|-%>
  var type = <%= area.ID %>;
        var marker<%= area.ID %> = new GMarker(new GLatLng(<%= key.LONG%>, <%= key.LANG %>));
        markerGroups[type].push(marker<%= area.ID %>);
        map.addOverlay(marker<%= area.ID %>);
        var html<%= key.ID %> ="<img src='www.something.com/something.jpg'" +
                 "width='128' height='128'/> <br/>" +
                 "Test test testing<br/>" +
                 " - <%= key.ID %> -";
    //    marker1.openInfoWindowHtml(html<%= key.ID %>);
        GEvent.addListener(marker<%= area.ID %>, "click", function() {
          marker1.openInfoWindowHtml(html<%= key.ID %>);
   
        });
       

 
<% end -%>
<% end -%>[/code]

This solution came to life through other threads in this forum, thanks!
I hope that someone finds it helpful.

Last edited by vaistik (2008-10-10 11:28:53)