Reputation: 95
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;
}
Above is my code sample. While I can access the Google Maps API URL online and see the full JSON results, when using it in my code, it will not run the 'sucess' function of my $.getJSON call. I can also save the results of the URL and use that file without issue.
Why can't I use the URL directly in my code?
Thanks.
Upvotes: 0
Views: 26
Reputation: 161384
If I run your code (fiddle), I get a javascript error:
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.
If you want to access the places API text search from javascript, use the Places Library of the Google Maps Javascript API v3
Upvotes: 2