如何捕获和解析从Google Maps v3 API返回的JSON?

时间:2011-11-30 11:02:13

标签: javascript google-maps google-maps-api-3

我写下了以下代码:

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);

如何从此通话中捕获响应?

正在回复回复。请看这张图片:http://i.imgur.com/RZAit.png

如何在变量中捕获此响应?

2 个答案:

答案 0 :(得分:1)

如果您使用的是V3,则应使用地理编码API: http://code.google.com/apis/maps/documentation/geocoding/

API不再返回JSON-P,因此您无法使用ajax在同一域上没有代理的情况下访问它。

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

结果和状态将发送到回调函数。

答案 1 :(得分:0)

我使用jQuery的getJSON()方法:http://api.jquery.com/jQuery.getJSON/,它会自动从您可以使用的响应中创建一个json对象。