2016-07-27 52 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