在
代码下方,place.name
显示通过地理位置找到的地点的名称。我应该能够使用place.formatted_address
列出地址,但这不起作用。我错过了什么?
https://developers.google.com/maps/documentation/javascript/places?csw=1#place_details_requests
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<script>
var map, placesList;
function initialize() {
var pyrmont = new google.maps.LatLng(40.062664, -83.069101);
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);
}
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) {
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,
phone:place.formatted_address,
position: place.geometry.location
});
placesList.innerHTML += '<li>' + place.name + '</li>';
placesList.innerHTML += '<li>' + place.formatted_address + '</li>';
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
您不请求地点详细信息,而是运行附近的搜索。
如文档所述,搜索响应将仅包含文本搜索的formatted_address
-属性