Jeewes
Jeewes

Reputation: 1363

Gmaps4rails: How to add attributes to json

I'm trying to add id attribute to my marker that I have created with Gmaps4rails gem (great gem btw), so that I could use that id to modify my list elements when the marker is clicked.

Now I have this:

@users.to_gmaps4rails do |user, marker|                                                                                                          
    marker.title user.name
    marker.json "\"id\": #{user.id}"
end

, but it does not seem to work.

I'm trying to read the id attribute like this:

for (var i = 0; i <  Gmaps.map.markers.length; ++i) {   
      google.maps.event.addListener(Gmaps.map.markers[i].serviceObject, 'click', function(event) {                 
        alert(event.id);   //<-------------Not working     
      });
    }

Any Ideas how I could store my user ID into the marker so that I could read it inside the click event block?

Upvotes: 0

Views: 482

Answers (1)

apneadiving
apneadiving

Reputation: 115521

Do:

for (var i = 0; i <  Gmaps.map.markers.length; ++i) {
  var marker = Gmaps.map.markers[i];
  google.maps.event.addListener(marker.serviceObject, 'click', onMarkerClick(marker, event));
}

function onMarkerClick(marker, event){
  return function(event){
    alert(marker.id);
  }
}

Upvotes: 2

Related Questions