【问题标题】:jQuery and Google Maps json responsejQuery 和谷歌地图 json 响应
【发布时间】:2011-04-14 06:58:28
【问题描述】:

我无法从 google maps api 获取地理位置信息

代码很简单

$.ajax({
    type: "GET",
    cache: false,
    url: "http://maps.googleapis.com/maps/api/geocode/json",
    dataType: "jsonp",
    data: {
        address: "Ljubljana " + "Slovenia",
        sensor: "false"
    },
    jsonpCallback:'json_response',
    success: function(data) {
        top.console.debug(data);
        $('#location_setter').dialog('close');
    },
    error: function() {
        alert("Error.");
    }
});


function json_response(data){
    alert("works");
}

我总是收到错误消息。 我也直接试过(我在某处读到应该在最后设置回调......

$.ajax({
    type: "GET",
    cache: true,
    url: "http://maps.googleapis.com/maps/api/geocode/json?address=Ljubljana Slovenia&sensor=false",
    dataType: "jsonp",
    jsonpCallback:'json_response',
    success: function(data) {
        top.console.debug(data);
        $('#location_setter').dialog('close');
    },
    error: function() {
        alert("Error.");
    }
});

请求的 url 格式正确:

http://maps.googleapis.com/maps/api/geocode/json?address=Ljubljana%20Slovenia&sensor=false&callback=json_response

它给了我正确的 json

请指教!

您可以在http://jsfiddle.net/PNad9/“玩”它

【问题讨论】:

    标签: jquery ajax json api maps


    【解决方案1】:

    如果它对某人有帮助...我放弃了,我完全改变了逻辑本身,现在它可以按预期工作:

    首先我将 google maps js 附加到正文中

    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.src = 'http://maps.google.com/maps/api/js?sensor=false&callback=get_longlat';
    $("body").append( script );
    

    如你所见,我指定了回调函数 get_longlat...

    所以我定义了这个函数并使用了谷歌的地理编码对象

    function get_longlat(address){
    
        var geocoder = new google.maps.Geocoder();
    
        if(!address){
            var p = US.get("location_postal");
            var c = US.get("location_city");
            var a = US.get("location_address");
            var address = a + ', ' + p + ' ' + c + ', Slovenia';
        }
    
        if (geocoder) {
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    US.set("location_lat", results[0].geometry.location.lat(), 60);
                    US.set("location_lon", results[0].geometry.location.lng(), 60);
    
                    $('#location_setter').dialog('close');
                } 
                else {
                    console.log('No results found: ' + status);
                }
            });
        }
    }
    

    里面的 US 变量是我们自己的 USER SETTINGS 命名空间

    希望对某人有所帮助

    【讨论】:

    • 谢谢伙计,这节省了我很多时间。
    【解决方案2】:

    Here is a good post 关于您遇到原始问题的行为的原因。您在 (http://maps.googleapis.com/maps/api/geocode/json) 向 google 发出请求的 URL 不返回 jsonp 可解析响应,它只返回一个原始 JSON 对象。 Google 拒绝将 JSON 包装在提供的回调中。

    据我所知,解决此问题的方法或多或少是向以下 URL 发出请求:http://maps.google.com/maps/geo,并在您的请求中包含 Google Maps API 密钥。此外,似乎有必要确保请求中的回调参数是查询字符串的最后一个参数。

    这是来自服务的可解析响应的 jsfiddle(即使它返回错误,因为没有提供有效的 Google Maps API 密钥):http://jsfiddle.net/sCa33/

    另一种选择是做你已经做过的事情,看起来,并使用谷歌的地理编码器对象来运行请求。

    【讨论】:

      【解决方案3】:

      我已经查看了问题,但我无法弄清楚发生了什么。我确实发现的一件事是,如果我将数据参数添加到错误中,然后 alert(data.status) 它会给出代码 200。我能找到的唯一地方是将其解析为 v2 的成功代码?!

      error: function(data) {
          alert(data.status);
      }
      

      我也不确定你是否可以使用那个 jsonp,因为阅读文档似乎在查询字符串上添加了额外的参数?当我删除它 jsonpcallback 并将数据类型更改为 json 时,响应代码开始显示 0。

      抱歉,帮不上忙,但我现在得去上班了。

      【讨论】:

        【解决方案4】:
        function codeAddress(address, title, imageURL) {
                    console.log(address);
        
                    geocoder.geocode({'address': address}, function (results, status) {
                        console.log(results);
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
        
                                var marker = new google.maps.Marker({
                                    map: map,
                                    position: results[0].geometry.location,
                                    icon: "",
                                    title: title
                                });
        
                            /* Set onclick popup */
                            var infowindow = new google.maps.InfoWindow({content: title});
                            google.maps.event.addListener(marker, 'click', function () {
                                infowindow.open(marker.get('map'), marker);
                            });
                        }
                        else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                            setTimeout(function () {
                                codeAddress(address, title, imageURL);
                            }, 250);
                        } else {
                            alert("Geocode was not successful for the following reason: " + status);
                        }
                    });
        

        【讨论】:

          猜你喜欢
          • 2013-03-03
          • 2018-02-05
          • 1970-01-01
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-19
          相关资源
          最近更新 更多