谷歌Places API的:不能够显示所有的搜索结果

问题描述:

我无法显示所有来自谷歌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的属性“长度”。任何线索?

最有可能你在网上越来越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://*.com/questions/11665684/more-than-20-results-by-pagination-with-google-places-api为同一个问题的答案。 – kaskader

+0

yeahh ..谢谢你kaskader – teekib

我已经完成了所有这些工作。感谢你们发帖,但我得到了解决方案。真的很好的解决方案。见下面的工作示例。

当调用此方法来检查是否有网页或不

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

//当下一页()请求完成

if(pagination.b == null){ 

//这里pagination.b有下一个页面标记。所以当发送最后一个请求时它会返回null。

createMarkers(placesList); 

}