簡體   English   中英

無論如何使用api將顯示數量限制為2條評論

[英]is there anyway to limit display number to 2 reviews on google places using api

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

似乎 Google 評論默認為 5 條評論(來源: 是否可以從 google place api 獲取最新的 5 條 Google 評論? )。

你可以只使用前兩個而忽略后三個嗎?

你應該能夠在你自己的代碼中做到這一點; 它不依賴於 API; 例如,如果您使用 Jquery 或 PHP,您可以遍歷數組並僅顯示索引為 0 和 1 的評論,例如僅顯示兩者而忽略其余部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM