【问题标题】:How to capture and parse JSON returned from Google Maps v3 API?如何捕获和解析从 Google Maps v3 API 返回的 JSON?
【发布时间】:2012-01-09 14:59:33
【问题描述】:

我已经写下了以下代码:

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

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

【问题讨论】:

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


【解决方案1】:

如果您使用的是 V3,则应为此使用 Geocoding 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);

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多