我知道有很多关于此的话题,但我无法在任何地方找到我的错误。我正在构建一个移动应用程序,我正在尝试查找位置并在列表中显示其名称。到目前为止,我的代码找到了我想要的所有位置,并为所有这些创建了标记,但是当我尝试编写名称时,它只写了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);
答案 0 :(得分:0)
您正在使用radar search,其中最多可检索200个结果但不支持pagination,这适用于其他类型的searches (NearbySearch, TextSearch),其中批量返回最多60个结果20 ..
雷达搜索请求
雷达搜索允许您按关键字,类型或名称搜索指定搜索半径内的位置。雷达搜索返回的结果比“附近搜索”或“文本搜索”更多,但结果包含的字段较少。您可以调用PlacesService.getDetails()以获取有关响应中任何位置的更多信息。
radarSearch()返回的PlaceResult对象仅包含以下属性:
- geometry.location
- place_id