Google Places API - 详细信息

问题描述:

我试图从谷歌地方抓取2个细节。评论和评级。问题是我似乎无法获得HTTP GET来返回任何东西。Google Places API - 详细信息

这里是我的代码:

$.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:意外的标记:

enter image description here

如果我点击链接,在那里需要我这个:

enter image description here

这里是Google Docs Link

我所做的:

  1. 设置我的API密钥。
  2. 找到我的公司场所ID:ChIJz153jtgN1oYRQeG2PvY5sWc

任何指针或方向,将不胜感激。

+1

Google没有为Places API Web服务启用CORS标头,因此您无法通过AJAX请求调用Web服务。您应该使用Maps JavaScript API的地方库来解决问题。只要按照@Prasanth Ravi的回答。 – xomena

尝试下面捣鼓的解决方案,

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网址和看看你的网络的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" 
} 

那么很可能它的权限问题,除此之外,如果成功,那么你需要检查你的代码。

希望这有助于。