How to capture and parse JSON returned from Google Maps v3 API?

I have written down the following code:

var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+position.coords.latitude+","+position.coords.longitude+"&sensor=false&callback=parseMe";
            console.log('URL is -- '+url);

            var scr = document.createElement('script');
            scr.src = url
            scr.type = 'text/javascript';
            document.getElementsByTagName('head')[0].appendChild(scr);

How do I capture the response from this call?

I am getting the response back. Please see this image: https://i.sstatic.net/XjPhq.png

How do I capture this response in a variable?

Upvotes: 2

Views: 5735

Answers (2)

Jørgen
Jørgen

Reputation: 9130

If you're on V3 you should use the Geocoding API for this: http://code.google.com/apis/maps/documentation/geocoding/

The API no longer returns JSON-P, so that you won't be able to access it with ajax without a proxy on the same domain.

var geocoder = new google.maps.Geocoder();
geocoder.geocode({ <put options here>}, callback);

results and status is sent to the callback function.

Upvotes: 1

Hoff
Hoff

Reputation: 39896

I'd use jQuery's getJSON() method: http://api.jquery.com/jQuery.getJSON/, it automatically creates a json object from the response that you can then use.

Upvotes: 0

Related Questions