jQuery和Google Maps json响应

| 我在从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.\");
    }
});
请求网址格式正确: http://maps.googleapis.com/maps/api/geocode/json?address=Ljubljana%20Slovenia&sensor=false&callback=json_response 它给了我正确的json 请指教! 您可以在http://jsfiddle.net/PNad9/上对其进行“播放”。     
已邀请:
如果它对某人有帮助...我放弃了,我完全改变了逻辑本身,现在它可以按预期运行: 首先,我将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 ... 所以我定义了此函数并使用了Google \的地理编码对象
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变量是用户设置的我们自己的名称空间 希望对别人有帮助     
这是一篇很好的文章,说明了您为什么要从原始问题中体验到这种行为。您在(http://maps.googleapis.com/maps/api/geocode/json)向Google提出请求的网址不会返回jsonp可解析的响应,而只会返回原始JSON对象。 Google拒绝将JSON包装在提供的回调中。 据我所知,解决此问题的方法或多或少是将您的请求发送到以下URL:http://maps.google.com/maps/geo,并在您的请求中包含Google Maps API密钥。同样,似乎有必要确保请求中的回调参数是查询字符串的最后一个参数。 这是该服务可解析响应的jsfiddle(尽管由于未提供有效的Google Maps API密钥而返回错误):http://jsfiddle.net/sCa33/ 另一个选择是做似乎已经做的事,并使用Google \的Geocoder对象运行请求。     
我已经看过问题了,但是我不知道发生了什么。我发现的一件事是,如果我向错误添加数据参数,然后发出alert(data.status),它将给出代码200。我唯一能找到的地方就是将其解析为v2的成功代码?
error: function(data) {
    alert(data.status);
}
另外我不确定您是否可以使用该jsonp,因为阅读文档似乎在查询字符串上添加了额外的参数?当我删除它jsonpcallback并将数据类型更改为json时,响应代码开始说0。 抱歉,没有太多帮助,但是我得现在去上班。     
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);
                }
            });
    

要回复问题请先登录注册