2010-07-31 111 views
1

我目前正在編寫一個廣泛使用谷歌地圖API(V3)的Rails 3應用程序。目前,每個標記的infowindow包含一個鏈接,將用戶帶到特定標記的頁面 - 即。 /宿/ 1。我的代碼的樣品是如下:在谷歌地圖infowindow AJAX鏈接

jQuery.getJSON("/places", function(json) { 
     if (json.length > 0) { 
     for (i=0; i<json.length; i++) { 
      var place = json[i]; 
      var category = json[i].tag; 
      addLocation(place,category); 
     } 
     } 
    }); 

    function addLocation(place,category) { 
    var marker = new google.maps.Marker({ 

位置:新google.maps.LatLng(place.lat,place.lng) 圖:圖, 標題:place.name, 圖標:圖像[ place.tag] });

 marker.mycategory = category; 
     gmarkers.push(marker); 

     google.maps.event.addListener(marker, 'click', function() { 
     if (infowindow) infowindow.close(); 
     infowindow = new google.maps.InfoWindow({ 
      content: "<h3>"+ place.name +"</h3><p>" + place.tag +"</p><a href='/places/"+place.id+"'>Show more!</a>" 
     }); 
     infowindow.open(map, marker); 
}); 
} 

這一切工作正常,到目前爲止,但我想,以消除用戶點擊信息窗口鏈接,通過引入AJAX調用後出現的頁面刷新。這可能嗎?任何幫助將非常感激!

編輯:

的.live功能是需要爲目標的信息窗口的鏈接,作爲信息窗口經常被創建後的DOM已準備就緒。所以我可以點擊鏈接,但我正在努力如何從我的map.js文件更新頁面的一部分。目前我有這樣的事情:

jQuery('#place_link').live('click', function() { 
    //alert("You clicked here."); 
    jQuery('#place_details').load('/places/"+place.id" #place_details'); 
    return false; 
}); 

...其中#place_details包含我想用AJAX調用更新標記的信息。正如我上面提到的那樣,.live部分工作正常,因爲單擊鏈接時(當未註釋掉時)會顯示警報。我該如何去實現這個目標?

回答

1

解決,從this question大量的幫助。在我的地圖中。js文件:

jQuery(document).ready(function(){ 
    mapInit(); 
    jQuery('#place_link').live('click', function() { 
    var url = jQuery(this).attr('href'); 
    jQuery.ajax({ 
     beforeSend: function(request) { request.setRequestHeader("Accept", "text/javascript"); }, 
     success: function(response) { jQuery('#place_details').empty().append(response); }, 
     type: 'get', 
     url: url 
    }); 
    return false; 
    }); 
}); 

筆者認爲:

<div id="map" style="width: 100%; height: 500px;"></div> 
<form action="#"> 
    Attractions: <input type="checkbox" id="attractionbox"/> &nbsp;&nbsp; 
    Food and Drink: <input type="checkbox" id="foodbox" /> &nbsp;&nbsp; 
    Hotels: <input type="checkbox" id="hotelbox" /> &nbsp;&nbsp; 
    Towns/Cities: <input type="checkbox" id="citybox" /><br /> 
</form> 

<p id="notice"><%= notice %></p> 

<div id="place_details"> 
    <%= render 'placeshow' %> 
</div> 

而且在我的控制器:

def show 
    @place = Place.find(params[:id]) 

    respond_to do |format| 
    format.html { render :action => "show" and return } 
    format.json { render :json => @places.all } 
    format.js { render :partial => "placeshow", :layout => false and return } 
    end 
end 

它的工作原理!

0

因此,如果我理解正確,您希望讓infowindow鏈接將AJAX調用回服務器以檢索「Show more!」的內容,對不對?

這大概有三部分,其中第三部分可能是你真正想問的是什麼?

  1. 啓用服務器端使用像JQuery.get()
  2. 攔截鏈接點擊做調用JavaScript調用
  3. 響應構建JavaScript調用服務器

對於部分3 I相信你至少有兩種選擇:

  1. 使用JQuery.click()創建JQuery事件處理程序(請參閱here),將一些CSS類或id添加到鏈接,以便您可以定位它/它們。
  2. onclick處理程序嵌入到鏈接中,並在其中執行AJAX調用。

在這兩種情況下,你的處理程序必須返回false停止瀏覽器做正常GET並刷新鏈接。

+0

感謝您的迴應 - 我真正在苦苦掙扎的是如何在服務器端使用rails進行組織...... – Budgie 2010-07-31 22:49:49