如何使用其 placeId 获取位置的详细信息,而无需提供 HTML 元素?



我很好奇如何使用placeId获得一个地方的详细信息,而不需要为它提供HTML元素。例如,HERE文档显示了所有需要HTML元素的示例,但我只需要调用它来在其他地方使用数据。

var request = {
  placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
};
service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
function callback(place, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    createMarker(place);
  }
}

试试google places api https://maps.googleapis.com/maps/api/place/details/json?placeid='PLACE_ID'&key=YOUR KEY'

如果您需要XML格式的响应,请将json更改为XML

您可以通过placeId获取地点详细信息,除了提供一个区域内的地点列表外,places服务还可以返回关于特定地点的详细信息。在地点搜索响应中返回某个地点后,可以使用其地点ID请求有关该地点的其他详细信息,例如完整地址、电话号码、用户评分和评论等。

位置ID提供了引用特定位置信息的可靠方法。使用地点ID的一种常用方法是搜索地点,然后使用返回的地点ID检索地点详细信息。

使用Google Places API Web Service,你可以通过做一个地点搜索请求来找到一个地点ID。

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.866, lng: 151.196},
zoom: 15
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '</div>');
infowindow.open(map, this);
});
}
});

最新更新