2016-07-27 69 views
0
function getCoffeeLatLon(inpLat, inpLon){ 
    var searchURL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=coffee&key=AIzaSyCKY43b8fpjLUuagtq1ZdSc477QwFuprew&location=15.7228486,-90.2348"; 
    var firstLat; 
    var firstLon; 
    var firstName; 
    var fullJSON= $.getJSON("https://maps.googleapis.com/maps/api/place/textsearch/json?query=coffee&key=AIzaSyCKY43b8fpjLUuagtq1ZdSc477QwFuprew&location=15.7228486,-90.2348", function(data){ 
              firstLat = data.results[0].geometry.location.lat; 
              firstLon = data.results[0].geometry.location.lng; 
              firstName = data.results[0].name; 
              console.log("checking"); 
              }); 

    var coffeeLatLonName = [firstLat, firstLon, firstName]; 
    return coffeeLatLonName; 
    } 

以上是我的代碼示例。儘管我可以在線訪問Google Maps API URL並查看完整的JSON結果,但在我的代碼中使用它時,它不會運行我的$ .getJSON調用的'sucess'函數。我也可以保存網址的結果並使用該文件而沒有問題。TextSearch不返回輸出(但URL有效)

爲什麼我不能直接在我的代碼中使用URL?

謝謝。

+0

我可以幫助我們,請發送完整的代碼 – KingRider

回答

2

如果我跑your code (fiddle),我得到一個JavaScript錯誤:

XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/textsearch/json?query=coffee&key=AIzaSyCKY43b8fpjLUuagtq1ZdSc477QwFuprew&location=15.7228486,-90.2348. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://fiddle.jshell.net' is therefore not allowed access. 

如果您要訪問從JavaScript的地方API文本搜索,使用Places Library of the Google Maps Javascript API v3

+0

謝謝,我現在就放棄這一點。 – JoshM