如何从Google Places API访问JSON响应

时间:2012-08-08 18:44:01

标签: jquery jsonp google-places-api

我正在尝试弄清楚如何使用Google Places API。我已经阅读了他们所有的文档。我只想弄清楚如何在JSON响应中获取数据。我不是想把响应输出到地图上。我想访问地名等等。

我试图在之前的帖子中使用$ .getJSON jquery函数(http://stackoverflow.com/questions/11642418/trying-to-use-google-places-api-with-jquerys-getjson -function),但我无法让浏览器相应地解析JSON响应,我在想,因为JSON响应在远程服务器中。我现在正在尝试使用JSONP,但我也有同样的错误。

<script type="text/javascript">

    function jasonPCall() {
        var url = 'https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=****my key****&callback=?';

        $.ajax({
            type: 'GET',
            url: url,
            async: false,
            jsonpCallback: 'jsonCallback',
            contentType: "application/json",
            dataType: 'jsonp',
            success: function (json) {

                console.log("We got a response.");
                console.dir(json.results[0].name);

            },
            error: function (e) {
                console.log("We didn't get a response.");
                console.log(e.message);
            }
        });
    }
</script>

我正在尝试做什么?如果是这样,您应该如何访问Google商家信息提供的JSON回复数据?有人可以发布一个正确的方法来做一个简单的例子吗?

1 个答案:

答案 0 :(得分:1)

据我所知,不支持通过javascript访问地点api。您必须使用Google Maps javascript sdk的Places库才能访问它。 地方文档的第一行是 https://developers.google.com/places/documentation/ 说 “希望在JavaScript应用程序中使用此服务?请查看Google Maps API v3的地方库。”