2011-07-29 14 views
0

來標記我想與鏈接標記添加標記,所以,當我點擊該標記,我會重定向到另一個頁面如何添加鏈接使用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

+0

Stackoverflow不是一個論壇。問題是問題,答案是答案。要爲您的問題添加更多信息,您應該編輯該問題。看看[FAQ]。 – dandan78

回答

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 %> 
+0

嗨apneadiving,thx爲你的答案。但我得到的錯誤消息_Gmaps4Rails未定義在代碼中partGmaps4Rails.callback = function(){。爲什麼發生? – fatimah

+0

因爲在調用gmaps4rails和「content_for:scripts」之後必須編寫該函數。 – apneadiving

+0

你可以在編輯問題後回答我的新問題嗎?因爲鏈接仍然無法重定向到其他頁面 – fatimah