从标记- gmaps4rails访问json属性。

时间:2022-03-25 15:56:51

I'm upgrading to gmaps4rails v2. I can't seem to access the marker json attributes from javascript. This worked in the previous version I was using (1.5.6)

我要升级到gmaps4rails v2。我似乎无法从javascript访问标记json属性。这在我使用的前一个版本中是有效的(1.5.6)

Specifically

具体地说

Buidling in controller: @users = User.all

控制器中的Buidling: @users = User.all。

@hash = Gmaps4rails.build_markers(@users) do |user, marker|
  marker.lat user.latitude
  marker.lng user.longitude
  marker.json({link: "someurl", current: true})
end

And in javascript:

在javascript中:

handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(<%=raw @hash.to_json %>);
  console.log(markers[0].link);
  console.log(markers[1].current);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
});

>undefined
>undefined

Thanks

谢谢

1 个解决方案

#1


3  

It is indeed the new behaviour: it doesn't change objects, choice is yours.

它确实是一种新的行为:它不会改变物体,选择是你的。

You could do:

你能做的:

markers_json = <%=raw @hash.to_json %>;
markers = _.map(markers_json, function(marker_json){
  marker = handler.addMarker(marker_json);
  _.extend(marker, marker_json);
  return marker;
});

#1


3  

It is indeed the new behaviour: it doesn't change objects, choice is yours.

它确实是一种新的行为:它不会改变物体,选择是你的。

You could do:

你能做的:

markers_json = <%=raw @hash.to_json %>;
markers = _.map(markers_json, function(marker_json){
  marker = handler.addMarker(marker_json);
  _.extend(marker, marker_json);
  return marker;
});