簡體   English   中英

Google Places API-詳細信息

[英]Google Places API - Details

我正在嘗試從Google地方獲取2個詳細信息。 評論和評分。 問題是我似乎無法使HTTP GET返回任何內容。

這是我的代碼:

$.ajax({
    url: "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJz153jtgN1oYRQeG2PvY5sWc&key=MyAPIKey",
    type: "GET", /* or type:"GET" or type:"PUT" */
    dataType: "jsonp",
    data: {
    },
    success: function (placesRequest) {
        console.log(placesRequest);
    },
    error: function () {
        console.log("Your code sucks, fix the HTTP request");
    }
});

錯誤:

未捕獲到的SyntaxError:意外令牌:

在此處輸入圖片說明

如果我單擊那里的鏈接,它將帶我到這里:

在此處輸入圖片說明

這是Google文檔鏈接

我所做的:

  1. 設置我的API密鑰。
  2. 找到我的公司,地點編號:ChIJz153jtgN1oYRQeG2PvY5sWc

任何指針或方向將不勝感激。

試試下面的小提琴解決方案,

https://jsfiddle.net/upsidown/yfawx50v/

function initialize() {
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: new google.maps.LatLng(0, 0),
        zoom: 15
    });

    var service = new google.maps.places.PlacesService(map);

    service.getDetails({
        placeId: 'ChIJz153jtgN1oYRQeG2PvY5sWc'
    }, function (place, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {

            // Create marker
            var marker = new google.maps.Marker({
                map: map,
                position: place.geometry.location
            });

            // Center map on place location
            map.setCenter(place.geometry.location);

            // Get DIV element to display opening hours
            var opening_hours_div = document.getElementById("opening-hours");

            // Loop through opening hours weekday text
            for (var i = 0; i < place.opening_hours.weekday_text.length; i++) {

                // Create DIV element and append to opening_hours_div
                var content = document.createElement('div');
                content.innerHTML = place.opening_hours.weekday_text[i];
                opening_hours_div.appendChild(content);
            }
        }
    });
}

initialize();

好吧,首先您需要查看語法是否錯誤,

https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJz153jtgN1oYRQeG2PvY5sWc&key=MyAPIKey

將ajax URL復制並粘貼到瀏覽器中,看看是否允許您的網絡ip使用該api密鑰,

如果返回這樣的錯誤,

{
   "error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address 223.255.255.255, with empty referer",
   "html_attributions" : [],
   "status" : "REQUEST_DENIED"
}

然后可能是它的權限問題,如果成功,那就需要檢查代碼。

希望這可以幫助。

暫無
暫無

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

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