Google Places API - 详细信息

时间:2017-07-17 22:17:39

标签: javascript jquery httprequest google-places-api

我试图从谷歌的地方抓取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");
    }
});

错误:

Uncaught SyntaxError:意外的令牌:

enter image description here

如果我点击那里的链接,我就会这样:

enter image description here

这是Google Docs Link

我做了什么:

  1. 设置我的API密钥。
  2. 找到我公司的地址:ChIJz153jtgN1oYRQeG2PvY5sWc
  3. 任何指针或方向将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试以下解决方案,

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();

答案 1 :(得分:0)

好的,首先你需要查看是否有任何语法错误,

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

然后可能是它的许可问题,除非成功,否则你需要检查你的代码。

希望这有助于。