如何对Google Maps API进行跨域AJAX调用?

发布于 2024-09-03 01:12:50 字数 297 浏览 2 评论 0 原文

我正在尝试对 $.getJSON 调用“noreferrer">Google 地图地理编码网络服务,但由于跨域安全问题,这不起作用。

我无法在网上弄清楚,但我读过一些有关 Google Javascript API 或 JSONP 的内容,但到目前为止还没有明确的答案......

有人能启发我吗?

谢谢!

I'm trying to make a jQuery $.getJSON call to the Google Maps Geocoding webservice, but this doesn't work because of cross-domain security issues.

I haven't been able to figure it out online, but I've read a bit about Google Javascript API or JSONP, but so far no clear answer...

Could anyone enlight me?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2024-09-10 01:12:50

我认为使用服务器端地理编码网络服务 当 Google 地图提供全功能客户端地理编码 API 对于 JavaScript。

首先,这会自动解决您的同源问题,此外,请求限制将按客户端 IP 地址计算,而不是按服务器 IP 地址计算,这对于受欢迎的网站来说可能会产生巨大的差异。

这是一个使用 JavaScript Geocoding API v3 的非常简单的示例:

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">     
   var geocoder = new google.maps.Geocoder();
   var address = 'London, UK';

   if (geocoder) {
      geocoder.geocode({ 'address': address }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0].geometry.location);
         }
         else {
            console.log("Geocoding failed: " + status);
         }
      });
   }    
</script>

如果由于某种原因您仍然想使用服务器端 Web 服务,您可以设置一个非常简单的 反向代理,可能使用 mod_proxy 如果您使用的是 Apache。这将允许您对 AJAX 请求使用相对路径,而 HTTP 服务器将充当任何“远程”位置的代理。

在 mod_proxy 中设置反向代理的基本配置指令是 ProxyPass。您通常会按如下方式使用它:

ProxyPass     /geocode/     http://maps.google.com/maps/api/geocode/

在这种情况下,浏览器可以向 /geocode/output?parameters 发出请求,但服务器将通过充当 http:// 的代理来提供此请求。 /maps.google.com/maps/api/geocode/output?parameters

I can see no advantage in using the Server-side Geocoding Web Service when Google Maps provides a full featured Client-side Geocoding API for JavaScript.

First of all, this automatically solves your same-origin problem, and in addition the request limits would be calculated per client IP address instead of of per server IP address, which can make a huge difference for a popular site.

Here's a very simple example using the JavaScript Geocoding API v3:

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">     
   var geocoder = new google.maps.Geocoder();
   var address = 'London, UK';

   if (geocoder) {
      geocoder.geocode({ 'address': address }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0].geometry.location);
         }
         else {
            console.log("Geocoding failed: " + status);
         }
      });
   }    
</script>

If for some reason you still want to use the server-side web-service, you could set up a very simple reverse proxy, maybe using mod_proxy if you are using Apache. This would allow you to use relative paths for your AJAX requests, while the HTTP server would be acting as a proxy to any "remote" location.

The fundamental configuration directive to set up a reverse proxy in mod_proxy is the ProxyPass. You would typically use it as follows:

ProxyPass     /geocode/     http://maps.google.com/maps/api/geocode/

In this case, the browser could make a request to /geocode/output?parameters but the server would serve this by acting as a proxy to http://maps.google.com/maps/api/geocode/output?parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文