使用jquery.getJson和Google的GeoCoding HTTP服务

Sco*_*ott 8 jquery geocoding getjson

Google提供了一个出色的REST界面,用于地址编码和地址反向地理编码.我的API密钥有效,如果我直接在浏览器地址中输入请求,它的效果很好.然而,以下jquery失败可怕,我没有理解为什么.希望你能在这里帮助我.

$.getJSON("http://maps.google.com/maps/geo?q="+ address+"&key="+apiKey+"&sensor=false&output=json",
  function(data, textStatus){
     console.log(data);
  });
Run Code Online (Sandbox Code Playgroud)

Google针对此服务的REST界面文档:http://code.google.com/apis/maps/documentation/geocoding/index.html

Sco*_*ott 18

这里的问题是我没有指定JSONP回调.正确的代码如下

$.getJSON("http://maps.google.com/maps/geo?q="+ address+"&key="+apiKey+"&sensor=false&output=json&callback=?",
  function(data, textStatus){
     console.log(data);
  });
Run Code Online (Sandbox Code Playgroud)

  • 它正在工作,因为你在查询结束时指定了自己的`callback`参数,但是如果你让jQuery处理它(通过指定`dataType:'jsonp'`)它就不会,请看:http://blog.mikecouturier的.com/2011/03/FIX-jQuery的JSONP请求到谷歌-maps.html (3认同)