gwendall
gwendall

Reputation: 920

parse google place json with javascript

for any reason this code doesn't seem to work

var request = "https://maps.googleapis.com/maps/api/place/search/json?";
request = request + "location="+lat+","+lng+"&";
request = request + "radius=500&";
request = request + "types=&";
request = request + "name=&";
request = request + "sensor=false&";
request = request + "key="+key;

$.getJSON(request, function(data){ alert(data); });

The string is valid and i get the result if i just load it in my browser. Do you see anything wrong here?

EDIT: alright, problem solved. The Google Places API actually does not accept ajax jsonp calls. I'll have to use their javascript API instead. See this thread for more details:

How do I use Google Places to get an array of Place names?


Thanks for your replies. So I now make the API call with the following code:

    $.ajax({
    url: "https://maps.googleapis.com/maps/api/place/search/json",
    dataType: 'jsonp',
    data: {
        location:lat+","+lng,
        radius:500,
        types:"",
        name:"",
        sensor:"false",
        key:key
    },
    success: function(data) {
        alert(data);
    }
});

Whiich doesn't show any alert box. By inspecting the code, I catch an error message:

   "html_attributions" : [],
  json:2Uncaught SyntaxError: Unexpected Token :

"results" : [ {

However, following this error message, in the console, the requested data actually shows in the "results" field... Any idea?

Upvotes: 4

Views: 2695

Answers (1)

bjornd
bjornd

Reputation: 22933

The Google Places API doesn't support JSONP requests.

Upvotes: 1

Related Questions