【问题标题】:parse google place json with javascript用 javascript 解析 google place json
【发布时间】:2023-03-11 11:34:02
【问题描述】:

由于任何原因,这段代码似乎不起作用

var request = "https://maps.googleapis.com/maps/api/place/search/json?";
request = request + "location="+lat+","+lng+"&";
request = request + "radius=500&";
request = request + "types=&";
request = request + "name=&";
request = request + "sensor=false&";
request = request + "key="+key;

$.getJSON(request, function(data){ alert(data); });

字符串是有效的,如果我只是在浏览器中加载它,我就会得到结果。你看这里有什么不对吗?

编辑:好的,问题解决了。 Google Places API 实际上不接受 ajax jsonp 调用。我将不得不改用他们的 javascript API。有关详细信息,请参阅此线程:

How do I use Google Places to get an array of Place names?


感谢您的回复。所以我现在使用以下代码进行 API 调用:

    $.ajax({
    url: "https://maps.googleapis.com/maps/api/place/search/json",
    dataType: 'jsonp',
    data: {
        location:lat+","+lng,
        radius:500,
        types:"",
        name:"",
        sensor:"false",
        key:key
    },
    success: function(data) {
        alert(data);
    }
});

Whiich 不显示任何警报框。通过检查代码,我发现了一条错误消息:

   "html_attributions" : [],
  json:2Uncaught SyntaxError: Unexpected Token :

“结果”:[ {

但是,在此错误消息之后,在控制台中,请求的数据实际上显示在“结果”字段中......有什么想法吗?

【问题讨论】:

标签: javascript jquery json google-maps


【解决方案1】:

Google Places API 不支持 JSONP 请求。

【讨论】:

  • 很高兴知道,但参考资料会很好了解更多信息。
  • @agrothe 你无法通过参考证明某些东西不存在,例如上帝。
猜你喜欢
  • 1970-01-01
  • 2019-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
相关资源
最近更新 更多