【问题标题】:Google Places Api: Not able to display all search resultsGoogle Places Api:无法显示所有搜索结果
【发布时间】:2012-10-12 09:37:45
【问题描述】:

我无法显示从 Google 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 个结果,但使用 settimeout() 函数只能显示 20..和 40...得到以下错误:未捕获的类型错误:无法读取 null 的属性“长度”。有什么线索吗?

【问题讨论】:

    标签: google-maps-api-3 google-places-api


    【解决方案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);
    }
    

    顺便说一句。 google.maps.GeocoderStatus.OVER_QUERY_LIMIT 的 Places 等效项是 google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT,您应该在使用 Places 时使用它。

    HTH

    【讨论】:

    • 仍然无法显示 60 个结果...任何其他方式
    • 你现在遇到了什么错误?在请求下一批之前,您需要添加一个睡眠功能,我上次错过了。就像文档说的那样:“nextPage() 一个将返回下一组结果的函数。执行搜索后,您必须等待两秒钟才能获得下一页结果。”。也可以在这里查看:stackoverflow.com/questions/11665684/… 以获得相同问题的答案。
    • 当然。这是一个完整的示例,没有详细信息,但返回了 6o 个结果:www-users.mat.umk.pl/~kaskader/places_60.html
    【解决方案2】:

    我已经做了所有这些努力。感谢你们发帖,但我得到了解决方案。真的是一个很好的解决方案。请参阅下面的工作示例。

    当你调用这个方法来检查它是否有页面时

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

    // 当 nextPage() 请求完成时

    if(pagination.b == null){  
    

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

    createMarkers(placesList);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多