2017-07-17 224 views
1

我試圖從谷歌地方抓取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

回答

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

好了首先你需要看是否有語法埃羅然後,

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

那麼很可能它的權限問題,除此之外,如果成功,那麼你需要檢查你的代碼。

希望這有助於。