Displaying Location Ratings using API

199 views
Skip to first unread message

jonas...@gmail.com

unread,
Mar 9, 2015, 1:57:26 PM3/9/15
to tripadvis...@googlegroups.com
I'm new to using the TripAdvisor API. I've gone through the process of applying for and receiving a development API key for a travel-related site featuring numerous hotels for the area. On the detail page for each location we would like to display the TripAdvisor rating for the location.

I've tested out my key by adding this in my browser address bar with my key that TripAdvisor provided me: http://api.tripadvisor.com/api/partner/2.0/location/89575?key=MYKEYHERE
And it shows a long page of data regarding a given location.

I've searched through a lot of documentation but can't seem to find anything on what to do with this in order to show the ratings bubbles. Does anyone have any examples of how I could show this in my HTML/PHP?


jonas...@gmail.com

unread,
Mar 9, 2015, 2:17:44 PM3/9/15
to tripadvis...@googlegroups.com
After further research I've found that the data is being sent as JSON, which I image I would use some sort of Javascript to display the information. I've done this before with Google Maps Javscript API but don't know how to go about this using TripAdvisor API. Any examples or information would be greatly appreciated.

Paritosh Arya

unread,
Mar 10, 2015, 3:13:37 AM3/10/15
to tripadvis...@googlegroups.com
You can use this code to display your ratings for businesses:

1) Form a request using multiple locations (i am using attrIds for it) or single location and make ajax calls to trip advisor

            //get attraction info
            var url = "https://api.tripadvisor.com/api/partner/2.0/location/" + attrIds + "/attractions?key=YOUR_KEY_HERE";

            $.ajax({
                type: "GET",
                cache: true,
                url: url,
                dataType: 'json',
                success: setAttractionsRating,
                error: function (msg) {
                    console.log(msg.responseText);
                }
            });
2)  Get data via callback method (setAttractionsRating in my case) and bind it to the respective fields on screen. I have looped through all controls on screen with class ratings where i need to display tripadvisor data.

        //set attraction rating from trip advisor fetched data
        $(".rating").each(function () {
            var ratingDiv = $(this);
            var locId = ratingDiv.attr("data-attrid");
            var matchFound = false;

            for (i = 0; i < response.data.length; i++) {
                if (locId == response.data[i].location_id) {
                    ratingDiv.attr("data-original", response.data[i].rating_image_url);
                    ratingDiv.next('a').attr("href", response.data[i].web_url);
                    ratingDiv.next('a').text(response.data[i].num_reviews + " reviews");
                    ratingDiv.next('a').next('a').attr("href", response.data[i].write_review);
                    ratingDiv.next('a').next('a').text("Write a Review");

                    attractionIds.push(response.data[i].location_id);
                    attrRatingImgUrl.push(response.data[i].rating_image_url);
                    attrReviewCount.push(response.data[i].num_reviews);
                    attrReviewUrl.push(response.data[i].web_url);
                    attrWriteReviewUrl.push(response.data[i].write_review);

                    break;
                }
            }
        });


Hope this helps!

Paritosh
Reply all
Reply to author
Forward
0 new messages