以下是我的代码。我希望以列表的形式获取格式化地址等所有地方的详细信息。但我得到getDetails()和引用错误。如果有人能告诉我我的错误在哪里
,我将非常感激 var map, placesList;
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: pyrmont,
zoom: 17
});
var request = {
location: pyrmont,
radius: 500,
types: ['store']
};
placesList = document.getElementById('places');
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
var request1 = {
reference: 'CnRkAAAAGnBVNFDeQoOQHzgdOpOqJNV7K9-c5IQrWFUYD9TNhUmz5-aHhfqyKH0zmAcUlkqVCrpaKcV8ZjGQKzB6GXxtzUYcP-muHafGsmW-1CwjTPBCmK43AZpAwW0FRtQDQADj3H2bzwwHVIXlQAiccm7r4xIQmjt_Oqm2FejWpBxLWs3L_RoUbharABi5FMnKnzmRL2TGju6UA4k'
};
service.getDetails(request1, createMarkers);
}
function callback(results, status, pagination) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
return;
} else {
createMarkers(results);
if (pagination.hasNextPage) {
var moreButton = document.getElementById('more');
moreButton.disabled = false;
google.maps.event.addDomListenerOnce(moreButton, 'click',
function () {
moreButton.disabled = true;
pagination.nextPage();
});
}
}
}
function createMarkers(places, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
var marker = new google.maps.Marker({
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});
placesList.innerHTML += '<li>' + place.name + '<br>' + place.formatted_address + '</li>';
bounds.extend(place.geometry.location);
map.fitBounds(bounds);
}
}
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:0)
您使用for loop
在函数places
中迭代createMarkers
,但只有对象:
Object {
address_components: Array[6],
formatted_address: "5/48 Pirrama Road, Pyrmont NSW, Australia",
formatted_phone_number: "(02) 9374 4000",
geometry: Object,
icon: "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png"…}
没有数组属性。我刚刚删除了代码周围的for loop
,并在列表中获取了地址并正确缩放到该地址。
您只要求一项特定服务。见文档:
除了提供区域内的地方列表外,地方服务还可以返回有关特定地点的详细信息。在地方搜索回复中返回地点后,其参考地产可用于请求有关该地点的其他详细信息,例如其完整地址,电话号码,用户评级和评论等。
function createMarkers(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
var bounds = new google.maps.LatLngBounds();
//for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
var marker = new google.maps.Marker({
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});
placesList.innerHTML += '<li>' + place.name + '<br>' + place.formatted_address + '</li>';
bounds.extend(place.geometry.location);
map.fitBounds(bounds);
//}
}
}
将callback
更改为以下内容:
function callback(results, status, pagination) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
return;
} else {
for (var i = 0; i < results.length; i++) {
var markerPlace = results[i];
createMarkers(markerPlace, status);
}
...
您正在处理具有相同功能createMarker()
的不同对象。因此,对于某些结果,undefined
获得了formatted_address
。在这些情况下,您可以使用vicinity
属性:
placesList.innerHTML += '<li>' + place.name + '<br>' +
(place.formatted_address ? place.formatted_address : place.vicinity)
+ '</li>';