admin管理员组文章数量:1290187
For whatever reason the following request gets no response from google. However if I paste the URL into the address bar it works fine. I'm not sure why this would happen. Is google filtering out requests by referrer headers?
;address=3122%2C%20Australia
$.ajax({
url:';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);
}
}
});
For whatever reason the following request gets no response from google. However if I paste the URL into the address bar it works fine. I'm not sure why this would happen. Is google filtering out requests by referrer headers?
http://maps.googleapis./maps/api/geocode/json?sensor=false&address=3122%2C%20Australia
$.ajax({
url:'http://maps.googleapis./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);
}
}
});
Share
Improve this question
asked Sep 30, 2010 at 0:01
BenbobBenbob
14.3k19 gold badges83 silver badges114 bronze badges
1 Answer
Reset to default 10It looks like you may be getting blocked by the Same Origin Policy.
You are trying to use the Server-side Geocoding Web Service when Google Maps provides a full featured Client-side Geocoding API for JavaScript. There is also a v2 version, but this has been deprecated recently.
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.
The client-side service should be quite easy to use. Here's a very simple geocoding example using the 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./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>
The above should print (-25.274398, 133.775136)
to the console.
本文标签: javascriptGoogle maps json geocodingno response in browserStack Overflow
版权声明:本文标题:javascript - Google maps json geocoding, no response in browser - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741491945a2381661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论