如何使用javascript在网站上显示Google的地点评论

问题描述 投票:0回答:1

我正在尝试显示我在该网站上工作的公司的所有google评论,但我似乎无法真正使它正常工作。我尝试为此使用以下代码:

<script>
    $(function() {
        var people = [];
        $.getJSON('https://maps.googleapis.com/maps/api/place/details/json?placeid=<?php echo $placeId ?>&key=<?php echo $api_key ?>', function(data) {
            $.each(data.reviews, function(i, f) {
                var tblRow = "<tr>" + "<td>" + f.author_name + "</td>" +
                "<td>" + f.rating + "</td>" + "<td>" + f.relative_time_description + "</td>" + "<td>" + f.text + "</td>" + "</tr>"
                $(tblRow).appendTo("#google-reviews");
            });
        });
    });
</script>

在这里,我应该得到一个类似于此json文件的响应:

{
   "html_attributions" : [],
   "result" : {
      ...
      "rating" : 4.5,
      "reference" : "CmRSAAAAjiEr2_A4yI-DyqGcfsceTv-IBJXHB5-W3ckmGk9QAYk4USgeV8ihBcGBEK5Z1w4ajRZNVAfSbROiKbbuniq0c9rIq_xqkrf_3HpZzX-pFJuJY3cBtG68LSAHzWXB8UzwEhAx04rgN0_WieYLfVp4K0duGhTU58LFaqwcaex73Kcyy0ghYOQTkg",
      "reviews" : [
         {
            "author_name" : "Robert Ardill",
            "author_url" : "https://www.google.com/maps/contrib/106422854611155436041/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/-T47KxWuAoJU/AAAAAAAAAAI/AAAAAAAAAZo/BDmyI12BZAs/s128-c0x00000000-cc-rp-mo-ba1/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "a month ago",
            "text" : "Awesome offices. Great facilities, location and views. Staff are great hosts",
            "time" : 1491144016
         }
      ],
      "types" : [ "point_of_interest", "establishment" ],
      "url" : "https://maps.google.com/?cid=10281119596374313554",
      "utc_offset" : 600,
      "vicinity" : "5, 48 Pirrama Road, Pyrmont",
      "website" : "https://www.google.com.au/about/careers/locations/sydney/"
   },
   "status" : "OK"
}

并且每次我遇到以下两个错误:

https://maps.googleapis.com/maps/api/place/details/json?placeid={placeid}&key={apikey} net::ERR_FAILED
Access to XMLHttpRequest at 'https://maps.googleapis.com/maps/api/place/details/json?placeid={placeid}&key={apikey}' from origin '{the site I work on}' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

是否有人知道如何解决此问题?

javascript json google-places-api
1个回答
0
投票

据我所知,为了通过客户端应用程序使用“地点详细信息”,您应该在Maps JavaScript API中使用“地点信息库”。这是文档:https://developers.google.com/maps/documentation/javascript/places

© www.soinside.com 2019 - 2024. All rights reserved.