我正在尝试使用jQuery的$.getJSON
方法调用Google Maps Geocoding webservice,但由于跨域安全问题而无法正常工作。
我在网上没有找到解决方案,但是我已经阅读了一些关于Google Javascript API或JSONP的文章,但目前仍没有清晰的答案...
有谁能帮助我吗?
谢谢!
我正在尝试使用jQuery的$.getJSON
方法调用Google Maps Geocoding webservice,但由于跨域安全问题而无法正常工作。
我在网上没有找到解决方案,但是我已经阅读了一些关于Google Javascript API或JSONP的文章,但目前仍没有清晰的答案...
有谁能帮助我吗?
谢谢!
当Google Maps为JavaScript提供完整功能的客户端地理编码API时,我看不出使用服务器端地理编码Web服务的任何优势。
首先,这自动解决了您的同源问题,此外请求限制将按客户端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>
ProxyPass /geocode/ http://maps.google.com/maps/api/geocode/
/geocode/output?parameters
发出请求,但服务器将通过充当代理来提供服务http://maps.google.com/maps/api/geocode/output?parameters
。