2

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.stack.imgur.com/XjPhq.png

How do I capture this response in a variable?

Tomas
  • 57,621
  • 49
  • 238
  • 373
Hrishikesh Choudhari
  • 11,617
  • 18
  • 61
  • 74
  • See this reply instead http://stackoverflow.com/questions/2921745/how-to-make-cross-domain-ajax-calls-to-google-maps-api – giorgio79 Mar 21 '13 at 21:20

2 Answers2

1

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.

Jørgen
  • 8,820
  • 9
  • 47
  • 67
0

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.

Hoff
  • 38,776
  • 17
  • 74
  • 99
  • Do you mean to use it this way? $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?latlng="+position.coords.latitude+","+position.coords.longitude+"&sensor=false", function(data){ alert(data); }); ?? – Hrishikesh Choudhari Nov 30 '11 at 11:15
  • I get this Same Origin Policy error when I try to use getJSON(). "XMLHttpRequest cannot load http://maps.googleapis.com/maps/api/geocode/json?latlng=12.971606,77.594376&sensor=false&callback=parseMe. Origin http://localhost is not allowed by Access-Control-Allow-Origin." – Hrishikesh Choudhari Nov 30 '11 at 11:21