1

I'm trying to get json response from google maps using this code:

var address = 'Sydney,NSW';
$.ajax({  
    type: "GET",  
    url: "http://maps.googleapis.com/maps/api/geocode/json?sensor=false",  
    dataType: "jsonp",
    data: ({"address":address}),
    success: function(json){
        if (json.status == "OK") {
           alert(":)");   
        } else {
            alert('wrong!');
        }
   }
});

It's working really good, but...

as a result i'm getting

Uncaught SyntaxError: Unexpected token : in Chrome and Error: no element found in FireFox..

it's seems that something wrong is with returned JSON, but it looking good:

{  
 "status": "OK",
 "results": [ {
 "types": [ "locality", "political" ],
 "formatted_address": "Sydney New South Wales, Australia",
   "address_components": [ {
   "long_name": "Sydney",
   "short_name": "Sydney",
   "types": [ "locality", "political" ]
  }, {
   "long_name": "New South Wales",
   "short_name": "New South Wales",
   "types": [ "administrative_area_level_1", "political" ]
}, {
  "long_name": "Australia",
  "short_name": "AU",
  "types": [ "country", "political" ]
} ],
"geometry": {
  "location": {
    "lat": -33.8689009,
    "lng": 151.2070914
  },
  "location_type": "APPROXIMATE",
  "viewport": {
    "southwest": {
      "lat": -34.1648540,
      "lng": 150.6948538
    },
    "northeast": {
      "lat": -33.5719182,
      "lng": 151.7193290
    }
  },
  "bounds": {
    "southwest": {
      "lat": -34.1692489,
      "lng": 150.5022290
    },
    "northeast": {
      "lat": -33.4245980,
      "lng": 151.3426361
    }
  }
}

} ] }

1 Answer 1

1

The return must be jsonp not json (the json should be wrapped into a function call)

Try using GM2 (looks like jsonp is not supported in GM3)

$.ajax({  
type: "GET",  
url: "http://maps.google.com/maps/geo?sensor=false&output=json&"+$.param({"q":address}),  
dataType: "jsonp",
success: function(json){
    if (json.Status.code == "200") {
       alert(":)");   
    } else {
        alert('wrong!');
    }

} });

Sign up to request clarification or add additional context in comments.

1 Comment

And it seemed, there's no option to do this via jsonp stackoverflow.com/questions/844341/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.