2016-07-27 4 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'機能は実行されません。私はURLの結果を保存し、問題なくそのファイルを使用することもできます。TextSearchは出力を返しませんが(URLは有効です)

私のコードでURLを直接使用できないのはなぜですか?

ありがとうございました。

+0

を使用し、JavaScriptから場所のAPIのテキスト検索にアクセスしたい場合は、完全なコードを送信してください。 – 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. 

あなたは私たちを助けることができるPlaces Library of the Google Maps Javascript API v3

+0

ありがとう、私は今これを行かせます。 – JoshM

関連する問題