2014-01-23 29 views
0

我使用地理編碼器來獲得一個基於郵政編碼的位置(我限制它只是美國)。一切工作正常,除非如果我輸入一個無效的zip(例如90123),它給了我一個默認值,而不是給ZERO_RESULTS。默認值恰好是德克薩斯州的Denton(對於所有無效代碼)。我怎樣才能防止這種情況發生,或者告訴一個代碼是否無效以避免這個問題?GeoCoder給出了無效的郵政編碼的默認值

我使用這個代碼:

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('|'); 
            --------------------------- 
          } 
         } 
        }); 
       } 

回答

0

目前它返回保德信大樓位於馬薩諸塞州波士頓。不知道爲什麼。

它發回你輸入的值。所以在results.address_components.short_name中,將是它返回的值。例如,如果我在「00000,United States」上搜索,則results.address_components.short_name將爲「02199」(上述建築的zip)。如果這與您提供的值不符,則可能是無效的壓縮文件。