简体   繁体   中英

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

I'm using webflow to display google reviews on my website using Google API, for now, everything ok but instead display 5 reviews I would like to display only 2

I already a code to display the reviews but is there anyway to add in my code to display only 2 reviews?

<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>

It appears Google Reviews defaults to 5 reviews (source: Is it possible to get latest five google reviews from google places api? ).

Could you use only the first two and ignore the last three?

You should be able to do this in your own code; it isn't API dependent; if you use Jquery or PHP for example you can loop through the array and only display the reviews with index 0 and 1 for example to only show the two and leave out the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM