【问题标题】:pagination undefined google maps : showing multiple placeDetails分页未定义的谷歌地图:显示多个地点详细信息
【发布时间】:2015-04-27 09:53:57
【问题描述】:

我知道有很多关于此的主题,但我无法在任何地方找到我的错误。我正在构建一个移动应用程序,我正在尝试查找地点并将其名称显示在列表中。到目前为止,我的代码找到了所有我想要的地方并为所有这些地方创建了标记,但是当我尝试写名字时,它只写了 15-20。在所有标记都放置在地图上之后,它开始写入名称。我搜索了解决方案,我找到了一个使用“分页”的解决方案,但是当我尝试它时,它给了我以下错误:“未捕获的 TypeError:无法读取未定义的属性 'hasNextPage'”

这是我显示地点的代码:

var map;
var nextPlace;

function initialize() {
    //get the current position of the device
    navigator.geolocation.getCurrentPosition(search, failPosition,{timeout:10000});
    document.addEventListener("backbutton", onBackKeyDown, false); 

}


//called if the position is not obtained correctly
function failPosition(error) {
  alert("Your position is not available, please check your settings");

}

function search(position) {
  var lat = position.coords.latitude;
  var lon = position.coords.longitude;
  var myPos= new google.maps.LatLng(lat, lon);
  var mapOptions = {
    zoom: 12,
    center: new google.maps.LatLng(lat, lon)
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

   var Types=new Array();
   var ty = window.localStorage.getItem("types");
   if(ty[0]!='0')
   {
    Types.push('restaurant');
   }
   if(ty[1]!='0')
   {
    Types.push('bar');
   }
   if(ty[2]!=0)
   {
    Types.push('night_club');
   }
  var request = {
    location: myPos,
    radius: String(window.localStorage.getItem("distance")),
    types: Types

  };

  var service = new google.maps.places.PlacesService(map);
  service.radarSearch(request, callback);

}

function callback(results, status, pagination) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {

    getPlacesDetails(results); 

    if (pagination.hasNextPage) {
      sleep:2;
       pagination.nextPage();
}
  else
  {
     $('li').click(function()
        {
          alert("clicked");
        })
  }


}
}

function getPlacesDetails(places)
{
  for (var i = 0, place; place = places[i];i++) {
    var request = {
      placeId: places[i].place_id
     };
     nextPlace=places[i].place_id;
     var service = new google.maps.places.PlacesService(map);

      service.getDetails(request,placesCallBack); 

      marker = new google.maps.Marker({
      position: places[i].geometry.location,
      map: map,
      title: 'your location'});
      }

}

}
function placesCallBack(place,status){
   if (status== google.maps.places.PlacesServiceStatus.OK) {
        $('#PlacesList').append('<li data="'+place.place_id+'"><h1>'+place.name+'</h1></li>').listview('refresh');
      }

  }


function onBackKeyDown() {
       window.location='MainPage.html';
}

google.maps.event.addDomListener(window, 'load', initialize);

【问题讨论】:

  • 检索页面有速率限制。你在考虑这个吗?使用您的代码,我得到“您的职位不可用,请检查您的设置”。
  • 那个函数是用来获取你的位置的,和搜索没有任何关系。我不确定您所说的“计算”是什么意思,我在“分页”中添加的代码是有人针对与我相同的问题提出的。

标签: javascript jquery google-maps google-maps-api-3 pagination


【解决方案1】:

您使用的是radar search,它最多检索200个结果并且不支持pagination,即对于其他类型的searches (NearbySearch, TextSearch),它以20个为一组最多返回60个结果..

雷达搜索请求

雷达搜索可让您按关键字、类型或名称搜索指定搜索范围内的地点。雷达搜索比附近搜索或文本搜索返回更多结果,但结果包含的字段更少。您可以调用 PlacesService.getDetails() 以获取有关响应中任何地点的更多信息。

radarSearch() 返回的 PlaceResult 对象仅包含以下属性:

  • geometry.location
  • place_id

【讨论】:

  • 我不知道radarSearch不支持分页。我尝试使用超时显示所有细节而不分页,但它不起作用,你知道我怎样才能显示所有细节?即使我不显示标记,显示的地点数量也是一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
相关资源
最近更新 更多