jQuery和谷歌地图json响应

DS_*_*per 6 api ajax maps jquery json

我从谷歌地图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");
}
Run Code Online (Sandbox Code Playgroud)

我总是得到一个错误.我也直接尝试过(我在某处读到应该在最后设置回调...

$.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.");
    }
});
Run Code Online (Sandbox Code Playgroud)

请求网址是否正确形成:

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

它给了我正确的json

请指教!

您可以在http://jsfiddle.net/PNad9/上 "玩"它

DS_*_*per 3

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

首先我将谷歌地图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 );
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我指定了回调函数 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);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

希望它可以帮助某人