反正有使用api在Google商家信息上将显示数量限制为2条评论

时间:2019-01-30 17:01:56

标签: google-maps google-maps-api-3

我正在使用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>

2 个答案:

答案 0 :(得分:-1)

看来Google评论默认为5条评论(来源:Is it possible to get latest five google reviews from google places api?)。

你能不能只使用前两个,而忽略过去三年?

答案 1 :(得分:-1)

您应该能够使用自己的代码执行此操作;它不依赖于API;例如,如果您使用Jquery或PHP,则可以遍历数组,仅显示索引为0和1的评论,例如仅显示两者,而忽略其余部分。