google-maps - 无论如何使用api将谷歌地方的显示数量限制为2条评论
问题描述
我正在使用 webflow 使用 Google API 在我的网站上显示 google 评论,目前一切正常,但显示 5 条评论我只想显示 2 条
我已经有一个代码来显示评论,但无论如何要在我的代码中添加只显示 2 条评论?
<script>
var map = document.getElementById('google-places');
function initMap() {
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'xxxxxxxxxxxxxxxxx'
}, function (places, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
reviewsArray = [];
reviewsArray.push(places.reviews);
for (var key in reviewsArray) {
var arr = reviewsArray[key];
for (i = 0; i < arr.length; i++) {
var review = arr[i];
var author = review.author_name;
var when = review.relative_time_description;
var comment = review.text;
var starNumber = review.rating;
var starPercentage = `${(starNumber / 5) * 100}%`;
console.log(starPercentage);
var profilePic = review.profile_photo_url;
// console.log(author + ', ' + when + ', ' + comment + ', ' + rating + ', ' + profilePic);
document.getElementById('google-places').innerHTML += ` ${author} ☆☆☆☆☆ ★★★★★
</div>
</div>
<div class="review-content-wrapper">
<div class="review-text">${comment}</div>
</div>
</div>
</div>
`
}
}
}
})
}
</script>
<!-- Replace XXX with your key that you created at https://console.cloud.google.com -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxx&libraries=places&callback=initMap">
</script>
解决方案
您应该能够在自己的代码中执行此操作;它不依赖于 API;例如,如果您使用 Jquery 或 PHP,您可以遍历数组并仅显示索引为 0 和 1 的评论,例如仅显示两者而忽略其余部分。
推荐阅读
- c# - 如何在 atom-editor 中更改 C# 版本?
- php - 下载一个在 PHP 中受密码保护的 Excel 工作表?
- javascript - 是否可以使用更短的辅助函数而不是 typeof something !== "undefined"?
- bert-language-model - BERT 训练检查点
- php - Codeigniter Form-Validation:验证输入是否等于某个字符串
- android - Android - ViewPager2 在调用 Diffutil.dispatchUpdatesTo 时将适配器位置重置为 0
- java - Spring-Batch 跳过异常如何适用于复合编写器
- hadoop - 如何在Hadoop中制作10G的随机文本?
- reactjs - 如何更改反应中的按钮名称?
- python - Django:如何将产品添加到订单模型,因为我尝试显示 ManyRelatedManager' 对象没有属性 'add' 或 filter 或类似错误