如何使用gmaps4rails gem添加指向标记的链接

时间:2011-07-29 13:42:05

标签: ruby-on-rails ruby ruby-on-rails-3 google-maps gmaps4rails

我想在标记中添加带链接的标记,因此,当我点击该标记时,我将重定向到另一个页面

有人知道吗?

由于


我已经添加了这样的代码:

在控制器中:

@json = @businesses.results.to_gmaps4rails do |object|
  "\"link\": \"#{root_url}\""
end

在视图中:

  <%= gmaps4rails(@json) %>
  <% content_for :scripts do %>
    <script type="text/javascript">
      function redirect_to(url) {
        window.location = url;
      };
      Gmaps4Rails.callback = function() {
        function say_yo(arg) { return function(){alert('yo '+ arg + '!' );};};
        for (var i = 0; i <  Gmaps4Rails.markers.length; ++i) {
          google.maps.event.addListener(Gmaps4Rails.markers[i].google_object, 'click', redirect_to(Gmaps4Rails.markers[i].link));
        }
      }
    </script>
  <% end %>

有什么不对吗?因为只有一个信息窗口显示我点击标记后(不重定向到任何页面)

enter image description here

1 个答案:

答案 0 :(得分:2)

首先在json中包含链接:

Model.all.to_gmaps4rails do |object|
  "\"link\": \"your link as string\""
end

然后在视图中添加额外的侦听器(请注意在调用gmaps方法后包含此内容):

<%= gmaps(whatever you need here) %>

<% content_for :scripts do %>
<script type="text/javascript">
function redirect_to(url) {
  window.location = url;
};
Gmaps4Rails.callback = function() { 
  function say_yo(arg) { return function(){alert('yo '+ arg + '!' );};};
    for (var i = 0; i <  Gmaps4Rails.markers.length; ++i) {
      google.maps.event.addListener(Gmaps4Rails.markers[i].google_object, 'click', redirect_to(Gmaps4Rails.markers[i].link));
    }
}
</script>
<% end %>