【发布时间】: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