Paul Dacus
Paul Dacus

Reputation: 486

Open & populate infowindow via ajax on "click" with Gmaps4Rails

I am trying to populate a Google map infowindow with content from a partial (via ajax) when the map marker is clicked.

apneadivings wiki post here has helped: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Adding-an-id-to-markers-and-access-them-later

I do have the id for the model instance I want to retrieve in the marker (from the source: "marker.json({ :id => user.id })"). But I cannot figure out how to use this to attach a listener to the markers click action, and then put the resulting HTML into the infowindow.

Upvotes: 0

Views: 372

Answers (1)

apneadiving
apneadiving

Reputation: 115541

After you create your markers:

markers = handler.addMarkers(...);

_.each(markers, function(marker){
  google.maps.event.addListener(marker.getServiceObject(), 'click', function(){
    $.get(expectedUrl)   
      .done(function(data){
        //only you know how `data` is structured
        var infowindow = new google.maps.InfoWindow({content: 'content' });
        infowindow.open( handler.getMap(), marker.getServiceObject());
      })
  })
});

Upvotes: 1

Related Questions