Search code examples
ruby-on-railsgmaps4rails

How to get information from the currently clicked marker in gmaps4rails?


I am trying to find a way to get information from the currently clicked marker on a map. For example, I would like to show the title and description of the currently clicked marker to show up on a different page. Any help is appreciated. Thank you.


Solution

  • You have to add a listener on click:

    var handler = Gmaps.build('Google');
    handler.buildMap({ provider: { }, internal: {id: 'map'}}, function(){
      // I assume this is the way you retrieve the amrkers array
      var json_data = <%=raw @hash.to_json %>;
      var markers = handler.addMarkers(json_data);
    
      _.each(json_data, function(json, index){
        var marker  = markers[index];
        json.marker = marker;
        google.maps.event.addListener(marker.getServiceObject(), 'click', function(){
          console.log(json);
          //whatever you need here
        });
      });
    
    });