2010-08-23 18 views
2

我有一個運行在Google App Engine上的Spring MVC 3應用程序(使用JSP)並將信息保存在數據存儲區中。我正在使用Google Maps API v3通過繪製形狀,着色等來投影地圖上的一些數據。我的數據庫將潛在地容納數百萬個條目。異步從數據存儲中提取數據並繪製地圖

我在想什麼最好的辦法是不斷從數據存儲中提取數據並將它們投影到地圖上,直到沒有更多的數據庫條目留給項目。我需要這樣做,以避免達到30秒的限制(並獲得DeadlineExceededException),但也爲了獲得良好的用戶體驗。

值得使用GWT嗎?

任何建議將是偉大的。

謝謝!

回答

0

您可以使用類似於此處描述的分頁技術光標:

Pagination in Google App Engine with Java

當與地圖加載你的頁面,有它做一個空白光標參數的AJAX請求。請求處理程序將獲取少量實體,然後返回包含它們和遊標的響應(如果還有實體)。

從客戶端javascript中,在顯示地圖上的項目後,如果響應中有一個遊標,則以遊標爲參數啓動一個新的請求。在請求處理程序中,如果提供了遊標,請在進行查詢時使用它。

這將設置一個連續的AJAX請求循環,直到所有項目已被提取並顯示在地圖上。

更新:

你可以寫返回JSON這樣的服務:

{ 
    items: 
    [ 
     { lat: 1.23, lon: 3.45, abc = 'def' }, 
     { lat: 2.34, lon: 4.56, abc = 'ghi' } 
    ], 
    cursor: '1234abcd' 
} 

因此,它包含的項目的陣列(經/緯度和任何其他信息,你需要每件)以及一個遊標(當最後一個實體被提取時它將爲null)。

然後,在客戶端我會建議使用jQueryajax功能,使Ajax調用,像這樣:

$(document).ready(function() 
{ 
    // first you may need to initialise the map - then start fetching items 
    fetchItems(null); 
}); 

function fetchItems(cursor) 
{ 
    // build the url to request the items - include the cursor as an argument 
    // if one is specified 
    var url = "/path/getitems"; 
    if (cursor != null) 
     url += "?cursor=" + cursor; 

    // start the ajax request 
    $.ajax({ 
     url: url, 
     dataType: 'json', 
     success: function(response) 
     { 
      // now handle the response - first loop over the items 
      for (i in response.items) 
      { 
       var item = response.items[i]; 
       // add something to the map using item.lat, item.lon, etc 
      } 

      // if there is a cursor in the response then there are more items, 
      // so start fetching them 
      if (response.cursor != null) 
       fetchItems(response.cursor); 
     }}); 
} 
+0

謝謝,這聽起來就像是我的初衷。你能提供一些代碼嗎?只是一個例子,讓我開始,因爲我從來沒有使用過AJAX。 – cxk 2010-08-24 09:04:02

+0

@cxk:我用一些示例代碼更新了我的回覆。 – 2010-08-25 01:09:36

相關問題