1

我使用谷歌地图自动完成功能。由于这并不总是返回 zip 并且不可能限制到多个国家/地区,因此我调用反向地理编码函数来检查 zip 和国家/地区,并检查结果是否仅在所需国家范围内:

var geocoder;

function in_array(needle, haystack) {
    for(var i in haystack) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function getPostal(loc, callback) {
    var countrys = new Array('DE', 'AT', 'CH');
    if (!geocoder) {
        geocoder = new google.maps.Geocoder();
    }
    var geocoderRequest = {
        'latLng': loc
    }
    geocoder.geocode(geocoderRequest, function(results, status) {
        var country = "";
        var zip = "";
        if (status == google.maps.GeocoderStatus.OK) {
            for(var i=0; i < results[0].address_components.length; i++) {
                var component = results[0].address_components[i];
                if(component.types[0] == "country") {
                    country = component.short_name;
                }
                if(component.types[0] == "postal_code") {
                    zip = component.short_name;
                }
            }               
            callback(1);
        }
        if (zip == "") callback(-1);
        if (country == "") callback(-2);
        if (!in_array(country, countrys)) callback(-3);
    }); 
}

$(function() {
    var checkPostal;
    var input = document.getElementById('search');
    var options = {
        types: ["geocode"]
    };
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();
        latlng = place.geometry.location;
        //reverse
        getPostal(place.geometry.location, function(checkPostal) { ;
            if (checkPostal == 1) {
                alert("OK");
            } else {
                if (checkPostal == -1) {
                    alert('no ZIP');
                } else if (checkPostal == -2) {
                    alert('no country');
                } else if (checkPostal == -3) {
                    alert('wrong country');
                }
            }
        });
    });
});

如果选择 fe Berlin,我只会得到“OK”。但是选择 fe New York 我首先得到一个“OK”,然后是“错误的国家”。如果选择 fe Westport, Ireland,我什至会得到“OK”、“no zip”和“wrong country”。

由于我只想在获得一个“OK”后采取进一步行动,我该怎么做?或者换一种方式质疑:如何获得一个单一的返回值?

谢谢费边

4

1 回答 1

1

当任何条件匹配时,您必须离开geocode-callback 并callback(1)在最后执行......这样它只会在没有条件匹配时执行:

geocoder.geocode(geocoderRequest, function(results, status) {
    var country = "";
    var zip = "";
    if (status == google.maps.GeocoderStatus.OK) {
        for(var i=0; i < results[0].address_components.length; i++) {
            var component = results[0].address_components[i];
            if(component.types[0] == "country") {
                country = component.short_name;
            }
            if(component.types[0] == "postal_code") {
                zip = component.short_name;
            }
        }    
        if (zip == "") {callback(-1);return;}
        if (country == "") {callback(-2);return;}
        if (!in_array(country, countrys)) {callback(-3); return;}      
        callback(1);
    }else{
      //do something when status is not OK
    }

 }); 
于 2013-10-10T11:06:57.873 回答