如何从Google Places API获取原始地点评分?

时间:2018-11-14 00:17:59

标签: google-maps

我正试图从Google地方信息中获得地点评分,但我坚持了这部分代码:

function initMap() {
  var map = new google.maps.Map();
  var service = new google.maps.places.PlacesService(map);

  service.getDetails({
      placeId: "ChIJ2fNZsMQ0GkcRCt39huGpijo"
    },
    function(place, status) {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        var rating = place.rating;
        $('.google_rating').html(rating);
      }
    });
}
<div class="rating" id="google_rating">
  <div class="google_rating"></div><span class="rating_star">&#9733;</span>
</div>

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

我不需要任何地图,仅需要特定地点的原始评分(地点ID)。我想念什么?

1 个答案:

答案 0 :(得分:0)

PlacesService构造函数采用google.maps.Map对象或HTMLDivElement:

  

PlacesService(attrContainer)

     

参数:
  attrContainer:HTMLDivElement |地图
  创建PlacesService的新实例,以在指定容器中呈现归因。

如果您不需要地图,则不要用地图来构造它,请使用HTMLDivElement像这样:

var service = new google.maps.places.PlacesService(document.getElementById("placeAttr"));

screenshot of result

代码段:

function initMap() {
  var service = new google.maps.places.PlacesService(document.getElementById("placeAttr"));

  service.getDetails({
      placeId: "ChIJ2fNZsMQ0GkcRCt39huGpijo"
    },
    function(place, status) {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        var rating = place.rating;
        $('.google_rating').html(rating);
      }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rating" id="google_rating">
  <div class="google_rating"></div><span class="rating_star">&#9733;</span>
</div>
<div id="placeAttr"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB1awNWLs_8JOTHWaC08TCUOo0r0PxPqvY&libraries=places&callback=initMap" async defer></script>