【问题标题】:Google Places API, how do I fetch the reviews?Google Places API,我如何获取评论?
【发布时间】:2015-02-27 18:47:42
【问题描述】:

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

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

【问题讨论】:

标签: javascript json google-places-api google-places


【解决方案1】:

您可以使用 Google Maps API:

https://maps.googleapis.com/maps/api/place/details/json?place_id=<place_id>&fields=reviews&key=<api_key>

更多:https://developers.google.com/places/web-service/details#PlaceDetailsResults

【讨论】:

  • 我可以从 google map api 获取所有评论吗?我的意思是没有任何限制...?
  • 我希望有一种方法来提取 place_id,但需要一个特定的审阅者。
  • 为了从 Google 获取所有评论,您必须使用 Google My Business API。您可以找到有关设置帐户的详细信息developers.google.com/my-business/content/overview
【解决方案2】:

一旦你有了一个地方的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>

【讨论】:

  • 谢谢你,Gaby,你帮我省了很多麻烦!
  • 如果我没有map怎么办?
  • @candlejack 是的。我需要按 place_id 获取评论,不需要地图。但现在看来我需要建立一个地图来做到这一点,并保持隐藏。谷歌应该在不使用地图的情况下为此提供一些类似 API 的休息。
猜你喜欢
  • 2016-01-09
  • 2014-09-06
  • 1970-01-01
  • 2015-06-26
  • 2019-02-17
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 2016-05-02
相关资源
最近更新 更多