【问题标题】:Google maps json geocoding, no response in browser谷歌地图json地理编码,浏览器无响应
【发布时间】:2010-09-30 00:01:17
【问题描述】:

无论出于何种原因,以下请求都没有得到 google 的响应。但是,如果我将 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);
                }
            }

        });

【问题讨论】:

    标签: javascript jquery google-maps google-maps-api-3


    【解决方案1】:

    您似乎被Same Origin Policy 屏蔽了。

    当 Google 地图为 JavaScript 提供全功能 Client-side Geocoding API 时,您正在尝试使用 Server-side Geocoding Web Service。还有一个v2 version,但最近已弃用。

    这会自动解决您的同源问题,此外,请求限制将按客户端 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>
    

    上面应该打印(-25.274398, 133.775136)到控制台。

    【讨论】:

    • 感谢您的快速回复!我让它工作并回来发现您已将其更改为使用geometry.location。干得好,我相信这对其他人有用。
    • 关于如何扭转这种情况的任何提示,我需要从 latlng 值中获取城镇/城市。
    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多