gpt4 book ai didi

javascript - Google Places API,如何获取评论?

转载 作者:搜寻专家 更新时间:2023-11-01 04:48:33 25 4
gpt4 key购买 nike

以前有几个人问过这个问题,但并不高兴。但是,似乎最近,谷歌提供了通过他们的 api 从你的谷歌地点获取评论的可用性。 https://developers.google.com/maps/documentation/javascript/places

我有显示我想要的确切 google 位置的 json 的 url,但是,我看不到有关如何仅从中获取评论的示例并且完全卡住了。他们的例子展示了如何显示 map ,但没有展示如何只获取评论。有没有人这样做过?如果是这样,是否有一个如何做的例子?谢谢。

最佳答案

一旦你有了一个地方的id,你就可以做

  var request = {
placeId: 'place-ID-here' // example: ChIJN1t_tDeuEmsRUsoyG83frY4
};

var service = new google.maps.places.PlacesService(map); // map is your map object

service.getDetails(request, function(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
console.log(place.reviews);
}
});

使用完整的工作示例更新 ( https://codepen.io/gpetrioli/pen/OmQyEE )

var map, service;

function initMap() {
var central_park = new google.maps.LatLng(40.764243, -73.973049);

map = new google.maps.Map(document.getElementById("map"), {
center: central_park,
zoom: 14
});

var request = {
location: central_park,
radius: "500",
types: ["food"]
};

service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, searchResult);
}

function searchResult(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
// show first result on map and request for details
var place = results[0];
var marker = new google.maps.Marker({
position: place.geometry.location,
map: map,
title: place.name
});
var infowindow = new google.maps.InfoWindow({
content: place.name
});
infowindow.open(map, marker);

service.getDetails({placeId: place.place_id}, function(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
let reviewEl = document.querySelector('.reviews');
for (let review of place.reviews){
let li = document.createElement('li');
li.innerHTML = `<div>Author: ${review.author_name}</div>
<em>${review.text}</em>
<div>Rating: ${review.rating} star(s)</div>`;
reviewEl.appendChild(li);
}
}
});
}
}
* {
box-sizing: border-box;
}

#map {
width: 500px;
height: 400px;
}

.reviews {
padding:0;
list-style:none;
}

.reviews li+li {
margin-top: 1em;
padding-top: 1em;
border-top: 1px solid black;
}
.reviews em{display:block;margin:0.3em 0;}
<div id="map"></div>
<ul class="reviews"></ul>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDbDfZUthAGL2BW3jg9xhWglf6HLpJQ1AU&callback=initMap&libraries=places"
async defer></script>

关于javascript - Google Places API,如何获取评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27725187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com