簡體   English   中英

谷歌放置 API getDetails() 服務不返回任何位置的評級

[英]Google places API getDetails() service not returning ratings for any location

我一直在嘗試構建一個自行車助手,並試圖顯示用戶輸入目的地的電話號碼和評級。

我有一些代碼可以讓我以下面的形式獲取整個請求(從 firefox 控制台復制)


address_components: Array(6) [ {…}, {…}, {…}, … ]
​
adr_address: "<span class=\"street-address\">Times Sq</span>, <span class=\"locality\">New York</span>, <span class=\"region\">NY</span>, <span class=\"country-name\">USA</span>"
​
formatted_address: "Times Sq, New York, NY, USA"
​
geometry: Object { location: {…}, viewport: {…} }
​
html_attributions: Array []
​
icon: "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/geocode-71.png"
​
icon_background_color: "#7B9EB0"
​
icon_mask_base_uri: "https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet"
​
name: "Times Square"
​
place_id: "ChIJO8I76FVYwokR2nYv8Wdvie0"
​
reference: "ChIJO8I76FVYwokR2nYv8Wdvie0"
​
types: Array [ "route" ]
​
url: "https://maps.google.com/?q=Times+Sq,+New+York,+NY,+USA&ftid=0x89c25855e83bc23b:0xed896f67f12f76da"
​
utc_offset: 
​
utc_offset_minutes: -240
​
vicinity: "Manhattan"

我獲得了我搜索的所有地點 ID 的合法詳細信息,例如電話號碼和地址。 但是,我沒有得到其中任何一個的評分。

我也嘗試了受歡迎的地方,但沒有評分字段。 我也沒有添加任何字段過濾器。 以下是我的參考請求設置。


function fetchmoredeets(addr) {
    var loc = addr.split(' ').join('+')
    const URL = `https://cors-anywhere.herokuapp.com/https://maps.googleapis.com/maps/api/place/textsearch/json?query=${loc}&key=(my_key)`
    // First get and parse the nearby regions

    async function fetcher(URL){
        const resp = await fetch(URL);
        return await resp.json()
    }

    var res, req;
    (async () => {
      req = await fetcher(URL);
      res = req.results;
      console.log("All deets helper:", res[0]);
      var deetsreq = {
              placeId: res[0].place_id,
            };

            var service = new google.maps.places.PlacesService(map);
            service.getDetails(deetsreq, callback);

            function callback(place, status) {
              if (status == google.maps.places.PlacesServiceStatus.OK) {
                console.log(place);
                document.getElementById("table-title").innerHTML = res[0].name
                var table = document.getElementById("deets-entries");
                var r = table.insertRow();
                r.insertCell().innerHTML =  place.address_components[1].short_name + ' ' + place.address_components[2].short_name; // addr

                r.insertCell().innerHTML= place.ratings; // rating
                if(place.formatted_phone_number==null){
                    r.insertCell().innerHTML= "NA";
                }
                else{
                    r.insertCell().innerHTML= place.formatted_phone_number;
                }
              }
            }


})()
}

上面,addr 是一個帶有地點地址的字符串(基於用戶從谷歌的地點自動完成 API 中選擇)

任何幫助都會很棒! 謝謝

您在示例中使用的地點 id ( ChIJO8I76FVYwokR2nYv8Wdvie0 ) 返回"types" : [ "route" ] 路線沒有評級。

相反,請使用包含評級的商家或其他類型地點的地點 ID。

使用Place ID Finder並搜索Times Square會返回不同的地點 id: ChIJmQJIxlVYwokRLgeuocVOGVU

使用此地點 ID 執行地點詳細信息請求:

https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJmQJIxlVYwokRLgeuocVOGVU&key=YOUR_API_KEY

返回"types" : [ "tourist_attraction", "point_of_interest", "establishment" ]"rating" : 4.7

暫無
暫無

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

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