2016-07-27 3 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; 
    } 

Oben ist mein Codebeispiel. Während ich online auf die Google Maps-API-URL zugreifen und die vollständigen JSON-Ergebnisse anzeigen kann, wird bei Verwendung in meinem Code die Funktion "Erfolg" meines $ .getJSON-Aufrufs nicht ausgeführt. Ich kann auch die Ergebnisse der URL speichern und diese Datei ohne Problem verwenden.TextSearch gibt die Ausgabe nicht zurück (aber die URL ist gültig)

Warum kann ich die URL nicht direkt in meinem Code verwenden?

Danke.

+0

ich uns helfen kann, bitte vollständige Code senden – KingRider

Antwort

2

Wenn ich your code (fiddle) laufen, erhalte ich eine Javascript-Fehler:

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. 

Wenn Sie die Orte API Textsuche von Javascript zugreifen möchten, verwenden Sie die Places Library of the Google Maps Javascript API v3

+0

Danke, ich werde es jetzt versuchen. – JoshM

Verwandte Themen