Make map marker direct link onclick for gmaps4rails

依然范特西╮ 提交于 2019-12-04 06:38:24

问题


Currently have the infowindow showing as the 'what happens' when the map marker is clicked, as so:

        google.maps.event.addListener(marker, 'click', function(){
            infowindow.open(Gmaps.map.map, marker);
        });

How would I get this to work to automatically link to the marker's SHOW page, ie. where is it possible to put in a code reference:

<a href='/controller/#{slug}'>#{title}</a> 

or

<%= link_to %> function

回答1:


For this kind of needs, I pass a block to the gmaps4rails method in the controller (doc here):

@json = User.all.to_gmaps4rails do |user, marker|
  marker.json "\"id\": #{user.id}"
  # or
  marker.json "\"link\": #{method_to_create_link}"
end

This way I can have any additional information I need to create a link or anything.

That said, you could update your listener this way:

base_url = "whatever you need";
google.maps.event.addListener(marker, 'click', function(){
   window.location(base_url + marker.id);
   // or
   window.location(marker.link);
});


来源:https://stackoverflow.com/questions/8608602/make-map-marker-direct-link-onclick-for-gmaps4rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!