Search code examples
ruby-on-railsruby-on-rails-3gmaps4rails

How to show the infowindow on mouseOver event


I'm upgrading an old Rails 2.1 app and am replacing the google_maps plugin with the Gmaps4Rails gem. The conversion has been pretty straight forward so far. The remaining task is to programmatically show and hide the markers infowindows on mouseOver of a list of addresses which I display beside the map.

My question is how do I get a handle to the marker to display the infowindow?

I found this question, but it seems to deal with the mouseOver of the marker. Essentially, I want to use a mouseOver event from an element outside of the map to show the infowindow of a marker.

Any help is appreciated.

This behavior seemed to be built into the google_maps plugin (as far as I can tell).


Solution

  • There is no builtin way to achieve this in gmaps4rails. But here are the relevant steps:

    1) In your controller

     Foo.your_scope.to_gmaps4rails do |obj, marker|
       # Add any custom elements here
       marker.json { :id => obj.id }
     end
    

    This will add the id of each element in the Gmaps.map.markers array.

    2) In your view

    Html:

    • add to each side element an attribute with the id of the related object

    Javascript:

    • Write a js function which retrieves a marker from it's id (basically, loop Gmaps.map.markers and check each element's id)

    • write a js function which observes your side elements mouseOver. In it's callback, find the related marker thanks to the above function and finally trigger this: foundMarker.infowindow.open(Gmaps.map.map, foundMarker.serviceObject)