【问题标题】:Call Google API from javascript code从 javascript 代码调用 Google API
【发布时间】:2014-09-14 08:47:16
【问题描述】:

我想从 JavaScript 代码调用 Google API。

我有一个 URL 可以像这样搜索附近的位置:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=ww.www,yy.yyy&radius=50&key=XXXXX

当我通过浏览器运行它时,我得到了正确的 JSON 结果。

但我想从我的 JavaScript 代码中获取 JSON 响应。

我不想在 Google 地图中显示这些位置,只是为了获取 JSON 对象并根据我的需要对其进行操作。

我尝试通过 Ajax 调用发送它,但它给出了错误。

【问题讨论】:

  • 您遇到了什么错误?

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


【解决方案1】:

这是我正在使用的:

//remember to add your API KEY to the url below. 

$.ajax({
    url: 'https://maps.googleapis.com/maps/api/geocode/json?address=UK&key=<KEY-GOES-HERE>', 
    type: 'GET',
    success: function(res) {
        alert(res.status); //responds with "OK"
    }
});

您需要使用 jQuery 标记才能运行,因为它使用 jQuery ajax 函数:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

【讨论】:

    【解决方案2】:

    使用 Maps-Javascript-API 的地点库:https://developers.google.com/maps/documentation/javascript/places#place_search_requests

    你也可以在没有地图的情况下使用这个图书馆的附近搜索(但是当你打印没有地图的数据时,你必须包括Logo

    您发布的 URL 可能不是通过 AJAX 请求的,因为服务器没有为 XDomainRequest 发送适当的标头

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 2021-10-07
      • 2012-05-26
      相关资源
      最近更新 更多