如何对Google Maps API进行跨网域AJAX调用? [英] How to make cross-domain AJAX calls to Google Maps API?

查看:403
本文介绍了如何对Google Maps API进行跨网域AJAX调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使用jQuery $。getJSON 调用 Google地图地理编码网络服务,但由于存在跨域安全问题,因此无法正常工作。

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.

我无法在网上找到,但我已经阅读了一些关于Google Javascript API或JSONP,但到目前为止没有明确的答案...

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...

任何人都可以指示我?

谢谢!

推荐答案

我没有看到使用服务器端地理编码Web服务,当Google地图提供完整功能的 JavaScript的客户端地理编码API

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.

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

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.

这里有一个非常简单的例子, JavaScript Geocoding API v3:

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>

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

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.

在mod_proxy中设置逆向代理的基本配置指令是ProxyPass。通常使用它如下:

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/

在这种情况下,浏览器可以请求 / geocode / output?parameters ,但服务器将通过充当代理 http://maps.google.com/maps/api/geocode/output?parameters code>。

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.

这篇关于如何对Google Maps API进行跨网域AJAX调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆