2012-10-12 32 views
2

我無法顯示所有來自谷歌Places API的請求的結果..谷歌Places API的:不能夠顯示所有的搜索結果

var Latlng = new google.maps.LatLng(Latitute, Longitute); 
     map = new google.maps.Map(document.getElementById('map_canvas'), { 
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
        center: Latlng, 
        zoom: 10}); 
     var request = {location: Latlng,types: [type]}; 
     var service = new google.maps.places.PlacesService(map); 
     service.search(request, callback); 
    } 

    function callback(results,status,pagination) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
    for (var i = 0; i < results.length; i++) { 
     createMarker(results[i]); 
     } 
     if (pagination.hasNextPage) { 
     pagination.nextPage(); }}} 

    function createMarker(place) { 
      var placeLoc = place.geometry.location; 
      var marker = new google.maps.Marker({map: map,zIndex: 100,position: place.geometry.location}); 
      var request = {reference : place.reference,}; 
      service = new google.maps.places.PlacesService(map); 
      service.getDetails(request, function detailsDisplay(details, status){ 
       if (status === google.maps.places.PlacesServiceStatus.OK) { 
        console.log(status); 
        $('#placesdata').append('<tr><td><a href='+details.url+'>' + details.name+ '</a></td></tr>'); 
       } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
        setTimeout(function() { 
        createMarker(place); 
        }, 200);}});} }; 

我使用分頁並獲得60倍的結果,但只能顯示20。 。和40使用settimeout()函數...獲取以下錯誤:未捕獲TypeError:無法讀取null的屬性「長度」。任何線索?

回答

1

最有可能你在網上越來越OVER_QUERY_LIMIT

if (status == google.maps.places.PlacesServiceStatus.OK) 

,因爲您已經創建了標記,這消耗了自己的配額。

在完成所有列表後添加相同的超時邏輯,以檢查此行上的google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT,嘗試對詳細請求進行排隊。

例如,你可以說:

if (pagination.hasNextPage) { 
    //add all places to the list 
    setTimeout('pagination.nextPage()',2000); 
} else { 
    createMarkers(placesList); 
} 

BTW。等效於google.maps.GeocoderStatus.OVER_QUERY_LIMIT的地方爲google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT,您應在使用地點時使用此地址。

HTH

+0

仍然無法顯示60個結果...任何其他方式 – teekib

+0

你現在得到什麼錯誤?在請求下一批之前,您需要添加睡眠功能,我最後一次錯過了。就像文檔所說:「nextPage()函數將返回下一組結果。執行搜索之後,您必須等待兩秒鐘,然後結果的下一頁纔可用。」也看看這裏:http://stackoverflow.com/questions/11665684/more-than-20-results-by-pagination-with-google-places-api爲同一個問題的答案。 – kaskader

+0

yeahh ..謝謝你kaskader – teekib

1

我已經完成了所有這些工作。感謝你們發帖,但我得到了解決方案。真的很好的解決方案。見下面的工作示例。

當調用此方法來檢查是否有網頁或不

if(pagination.hasNextPage){ 
    pagination.nextPage(); // let this do recursive loop against request. 
} 

//當下一頁()請求完成

if(pagination.b == null){ 

//這裏pagination.b有下一個頁面標記。所以當發送最後一個請求時它會返回null。

createMarkers(placesList); 

}