【问题标题】:GeoCoder gives default value for invalid zip codesGeoCoder 为无效的邮政编码提供默认值
【发布时间】:2014-01-23 20:45:30
【问题描述】:

我正在使用地理编码器根据邮政编码获取位置(我将其限制在美国)。一切正常,除了如果我输入了一个无效的 zip(例如 90123),它会给我一个默认值而不是给ZERO_RESULTS。默认值恰好是德克萨斯州丹顿(对于所有无效代码)。如何防止这种情况发生,或判断代码是否无效以避免此问题?

我正在使用此代码:

geocoder.geocode({ 'address': zip,  componentRestrictions: {country: 'us'} }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
                        jQuery('#zipErrorId').html("INVALID");
                    }
                    if (status == google.maps.GeocoderStatus.OK) {
                        geocoder.geocode({'latLng': results[0].geometry.location}, function(results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            if (results[1]) {
                                var cityState = getCityState(results);
                                // like Apple Valley, Minnesota
                                var cityStateArr = cityState.split('|');
                                   ---------------------------
                            }
                        }
                    });
                }

【问题讨论】:

    标签: zip geocoding google-geocoder


    【解决方案1】:

    目前它正在返回马萨诸塞州波士顿的保诚大厦。不知道为什么。

    它会发回您输入的值。所以在 results.address_components.short_name 中,将是它返回的值。例如,如果我搜索“00000,United States”,results.address_components.short_name 将是“02199”(上述建筑物的 zip)。如果这与您提供的值不匹配,则可能是无效的 zip。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      相关资源
      最近更新 更多