我正试图从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">★</span>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB1awNWLs_8JOTHWaC08TCUOo0r0PxPqvY&libraries=places&callback=initMap" async defer></script>
我不需要任何地图,仅需要特定地点的原始评分(地点ID)。我想念什么?
答案 0 :(得分:0)
PlacesService
构造函数采用google.maps.Map
对象或HTMLDivElement:
PlacesService(attrContainer)
参数:
attrContainer:HTMLDivElement |地图
创建PlacesService的新实例,以在指定容器中呈现归因。
如果您不需要地图,则不要用地图来构造它,请使用HTMLDivElement像这样:
var service = new google.maps.places.PlacesService(document.getElementById("placeAttr"));
代码段:
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">★</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>