开发者

Google geocode API ajax

开发者 https://www.devze.com 2023-04-12 15:24 出处:网络
I use jquery 1.6.4 and I just try to get city corresponding to a zipcode, so I want to use the Google Geocode API.

I use jquery 1.6.4 and I just try to get city corresponding to a zipcode, so I want to use the Google Geocode API.

$(document).ready(function() {
    $("#zipcode").keypress(function(){      开发者_如何学Python        
        $.getJSON(
            "http://maps.googleapis.com/maps/api/geocode/json",
            {
                sensor: false,
                address: "france"
            },
            function(data) {
                alert(data.status);
            }
        );
    });
});

But It doesn't work, firebug give me a status code 200 but not result. If I ask directly to the API, It work... So what I'm doing wrong ?


I build my solution by following this post

My solution using Jquery and google maps class

$("#zipcode").keyup(function() {
    var geocoder = new google.maps.Geocoder();
var address = $(this).val() + ', France';
if (geocoder) {
    geocoder.geocode({ 'address': address }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        for (var i=0;i<results[0].address_components.length;i++) {
            if (results[0].address_components[i].types[0] == 'locality')
            $('#city').val(results[0].address_components[i].long_name)
        }
    }
    });
}
}); 
0

精彩评论

暂无评论...
验证码 换一张
取 消