Key*_*eyo 2 javascript jquery google-maps google-maps-api-3
无论出于何种原因,以下请求都没有得到谷歌的回复.但是,如果我将URL粘贴到地址栏中,它可以正常工作.我不确定为什么会这样.谷歌是否过滤了引荐来源标题的请求?
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=3122%2C%20Australia
$.ajax({
url:'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='+post_code+encodeURIComponent(', Australia'),
success:function(data) {
alert(data);
if(data['status'] == 'OK') {
var location = data['results'][0]['geometry']['location'];
map.panTo(location['lat']+':'+location['lng']);
map.setZoom(8);
}
}
});
Run Code Online (Sandbox Code Playgroud)
Dan*_*llo 10
您可能会被同源政策阻止.
当Google Maps 为JavaScript 提供全功能的客户端地理编码API时,您正尝试使用服务器端地理编码Web服务.还有一个v2版本,但最近已被弃用.
这会自动解决您的同源问题,此外,将根据客户端IP地址而不是每服务器IP地址计算请求限制,这可能会对流行网站产生巨大影响.
客户端服务应该很容易使用.这是使用v3 API的一个非常简单的地理编码示例:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps Geocoding Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
if (geocoder) {
geocoder.geocode({ 'address': 'Australia' }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
}
else {
console.log('No results found: ' + status);
}
});
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
上面应该打印(-25.274398, 133.775136)到控制台.