如何快速确定给定邮政编码的状态?

时间:2015-03-03 00:12:59

标签: javascript php geo zipcode

我不需要城市或地址,只需要国家。如果可能的话,我不想进行API调用。优先考虑的是轻量级解决方案,理想情况下只需使用Javascript。

我有一个用户输入zipcode,我希望根据邮政编码所处的状态显示一段文字。我知道查找City 和州,对于这样的API,例如USPS公开的API可能是最好的。但是为了匹配状态,也许仅仅是前三个数字,解决方案(我认为)应该简单轻巧。

理想情况下的Javascript。 PHP也可以工作。

5 个答案:

答案 0 :(得分:10)

美国邮政编码数据实际上足够稳定如果只需要状态(不是城市或其他任何),您可以在不点击api或数据库的情况下执行此操作 。

这是一个轻量级的JS解决方案:

function getState(zipcode) {

    // Ensure param is a string to prevent unpredictable parsing results
    if (typeof zipcode !== 'string') {
        console.log('Must pass the zipcode as a string.');
        return;
    }

    // Ensure you don't parse codes that start with 0 as octal values
    const thiszip = parseInt(zipcode,10); 

    // Code blocks alphabetized by state
    if (thiszip >= 35000 && thiszip <= 36999) {
        thisst = 'AL';
        thisstate = 'Alabama';
        }
    else if (thiszip >= 99500 && thiszip <= 99999) {
        thisst = 'AK';
        thisstate = 'Alaska';
        }
    else if (thiszip >= 85000 && thiszip <= 86999) {
        thisst = 'AZ';
        thisstate = 'Arizona';
        }
    else if (thiszip >= 71600 && thiszip <= 72999) {
        thisst = 'AR';
        thisstate = 'Arkansas';
        }
    else if (thiszip >= 90000 && thiszip <= 96699) {
        thisst = 'CA';
        thisstate = 'California';
        }
    else if (thiszip >= 80000 && thiszip <= 81999) {
        thisst = 'CO';
        thisstate = 'Colorado';
        }
    else if (thiszip >= 6000 && thiszip <= 6999) {
        thisst = 'CT';
        thisstate = 'Connecticut';
        }
    else if (thiszip >= 19700 && thiszip <= 19999) {
        thisst = 'DE';
        thisstate = 'Deleware';
        }
    else if (thiszip >= 32000 && thiszip <= 34999) {
        thisst = 'FL';
        thisstate = 'Florida';
        }
    else if (thiszip >= 30000 && thiszip <= 31999) {
        thisst = 'GA';
        thisstate = 'Georgia';
        }
    else if (thiszip >= 96700 && thiszip <= 96999) {
        thisst = 'HI';
        thisstate = 'Hawaii';
        }
    else if (thiszip >= 83200 && thiszip <= 83999) {
        thisst = 'ID';
        thisstate = 'Idaho';
        }
    else if (thiszip >= 60000 && thiszip <= 62999) {
        thisst = 'IL';
        thisstate = 'Illinois';
        }
    else if (thiszip >= 46000 && thiszip <= 47999) {
        thisst = 'IN';
        thisstate = 'Indiana';
        }
    else if (thiszip >= 50000 && thiszip <= 52999) {
        thisst = 'IA';
        thisstate = 'Iowa';
        }
    else if (thiszip >= 66000 && thiszip <= 67999) {
        thisst = 'KS';
        thisstate = 'Kansas';
        }
    else if (thiszip >= 40000 && thiszip <= 42999) {
        thisst = 'KY';
        thisstate = 'Kentucky';
        }
    else if (thiszip >= 70000 && thiszip <= 71599) {
        thisst = 'LA';
        thisstate = 'Louisiana';
        }
    else if (thiszip >= 3900 && thiszip <= 4999) {
        thisst = 'ME';
        thisstate = 'Maine';
        }
    else if (thiszip >= 20600 && thiszip <= 21999) {
        thisst = 'MD';
        thisstate = 'Maryland';
        }
    else if (thiszip >= 1000 && thiszip <= 2799) {
        thisst = 'MA';
        thisstate = 'Massachusetts';
        }
    else if (thiszip >= 48000 && thiszip <= 49999) {
        thisst = 'MI';
        thisstate = 'Michigan';
        }
    else if (thiszip >= 55000 && thiszip <= 56999) {
        thisst = 'MN';
        thisstate = 'Minnesota';
        }
    else if (thiszip >= 38600 && thiszip <= 39999) {
        thisst = 'MS';
        thisstate = 'Mississippi';
        }
    else if (thiszip >= 63000 && thiszip <= 65999) {
        thisst = 'MO';
        thisstate = 'Missouri';
        }
    else if (thiszip >= 59000 && thiszip <= 59999) {
        thisst = 'MT';
        thisstate = 'Montana';
        }
    else if (thiszip >= 27000 && thiszip <= 28999) {
        thisst = 'NC';
        thisstate = 'North Carolina';
        }
    else if (thiszip >= 58000 && thiszip <= 58999) {
        thisst = 'ND';
        thisstate = 'North Dakota';
        }
    else if (thiszip >= 68000 && thiszip <= 69999) {
        thisst = 'NE';
        thisstate = 'Nebraska';
        }
    else if (thiszip >= 88900 && thiszip <= 89999) {
        thisst = 'NV';
        thisstate = 'Nevada';
        }
    else if (thiszip >= 3000 && thiszip <= 3899) {
        thisst = 'NH';
        thisstate = 'New Hampshire';
        }
    else if (thiszip >= 7000 && thiszip <= 8999) {
        thisst = 'NJ';
        thisstate = 'New Jersey';
        }
    else if (thiszip >= 87000 && thiszip <= 88499) {
        thisst = 'NM';
        thisstate = 'New Mexico';
        }
    else if (thiszip >= 10000 && thiszip <= 14999) {
        thisst = 'NY';
        thisstate = 'New York';
        }
    else if (thiszip >= 43000 && thiszip <= 45999) {
        thisst = 'OH';
        thisstate = 'Ohio';
        }
    else if (thiszip >= 73000 && thiszip <= 74999) {
        thisst = 'OK';
        thisstate = 'Oklahoma';
        }
    else if (thiszip >= 97000 && thiszip <= 97999) {
        thisst = 'OR';
        thisstate = 'Oregon';
        }
    else if (thiszip >= 15000 && thiszip <= 19699) {
        thisst = 'PA';
        thisstate = 'Pennsylvania';
        }
    else if (thiszip >= 300 && thiszip <= 999) {
        thisst = 'PR';
        thisstate = 'Puerto Rico';
        }
    else if (thiszip >= 2800 && thiszip <= 2999) {
        thisst = 'RI';
        thisstate = 'Rhode Island';
        }
    else if (thiszip >= 29000 && thiszip <= 29999) {
        thisst = 'SC';
        thisstate = 'South Carolina';
        }
    else if (thiszip >= 57000 && thiszip <= 57999) {
        thisst = 'SD';
        thisstate = 'South Dakota';
        }
    else if (thiszip >= 37000 && thiszip <= 38599) {
        thisst = 'TN';
        thisstate = 'Tennessee';
        }
    else if ( (thiszip >= 75000 && thiszip <= 79999) || (thiszip >= 88500 && thiszip <= 88599) ) {
        thisst = 'TX';
        thisstate = 'Texas';
        }
    else if (thiszip >= 84000 && thiszip <= 84999) {
        thisst = 'UT';
        thisstate = 'Utah';
        }
    else if (thiszip >= 5000 && thiszip <= 5999) {
        thisst = 'VT';
        thisstate = 'Vermont';
        }
    else if (thiszip >= 22000 && thiszip <= 24699) {
        thisst = 'VA';
        thisstate = 'Virgina';
        }
    else if (thiszip >= 20000 && thiszip <= 20599) {
        thisst = 'DC';
        thisstate = 'Washington DC';
        }
    else if (thiszip >= 98000 && thiszip <= 99499) {
        thisst = 'WA';
        thisstate = 'Washington';
        }
    else if (thiszip >= 24700 && thiszip <= 26999) {
        thisst = 'WV';
        thisstate = 'West Virginia';
        }
    else if (thiszip >= 53000 && thiszip <= 54999) {
        thisst = 'WI';
        thisstate = 'Wisconsin';
        }
    else if (thiszip >= 82000 && thiszip <= 83199) {
        thisst = 'WY';
        thisstate = 'Wyoming';
        }
    else {
        thisst = 'none';
    }
   return thisst;
}

非常感谢@ kevin-boucher和@ abaldwin99帮助解析较小的新英格兰代码并避免了他们的答案here的可怕的八进制评估错误。

还要感谢大部分原始代码转到this useful page

答案 1 :(得分:2)

您可以使用一系列范围返回给定邮政编码的最可能状态。

这不是验证者 - 并非范围内的每个数字实际上都被指定为邮政编码,并且将来可能会添加新的范围。

function stateFromZip(z){
    z= parseInt(z, 10);// removes leading '0'
    if(z<1001 || z>99950) return null;
    var i= 69, next, s, 
    zs= [
        [1001, 2791, 'Massachusetts'], [2801, 2940, 'Rhode Island'], [3031, 3897, 'New Hampshire'], 
        [3901, 4992, 'Maine'], [5001, 5495, 'Vermont'], [5501, 5544, 'Massachusetts'], 
        [5601, 5907, 'Vermont'], [6001, 6389, 'Connecticut'], [6390, 6390, 'New York'], 
        [6401, 6928, 'Connecticut'], [7001, 8989, 'New Jersey'], [10001, 14975, 'New York'], 
        [15001, 19640, 'Pennsylvania'], [19701, 19980, 'Delaware'], [20001, 20039, 'Dist. of Columbia'], 
        [20040, 20167, 'Virginia'], [20042, 20599, 'Dist. of Columbia'], [20331, 20331, 'Maryland'], 
        [20335, 20797, 'Maryland'], [20799, 20799, 'Dist. of Columbia'], [20812, 21930, 'Maryland'], 
        [22001, 24658, 'Virginia'], [24701, 26886, 'West Virginia'], [27006, 28909, 'North Carolina'], 
        [29001, 29948, 'South Carolina'], [30001, 31999, 'Georgia'], [32004, 34997, 'Florida'], 
        [35004, 36925, 'Alabama'], [37010, 38589, 'Tennessee'], [38601, 39776, 'Mississippi'], 
        [39901, 39901, 'Georgia'], [40003, 42788, 'Kentucky'], [43001, 45999, 'Ohio'], 
        [46001, 47997, 'Indiana'], [48001, 49971, 'Michigan'], [50001, 52809, 'Iowa'], 
        [53001, 54990, 'Wisconsin'], [55001, 56763, 'Minnesota'], [57001, 57799, 'South Dakota'], 
        [58001, 58856, 'North Dakota'], [59001, 59937, 'Montana'], [60001, 62999, 'Illinois'], 
        [63001, 65899, 'Missouri'], [66002, 67954, 'Kansas'], [68001, 68118, 'Nebraska'], 
        [68119, 68120, 'Iowa'], [68122, 69367, 'Nebraska'], [70001, 71232, 'Louisiana'], 
        [71233, 71233, 'Mississippi'], [71234, 71497, 'Louisiana'], [73001, 73199, 'Oklahoma'], 
        [73301, 73301, 'Texas'], [73401, 74966, 'Oklahoma'], [75001, 75501, 'Texas'], 
        [75502, 75502, 'Arkansas'], [75503, 79999, 'Texas'], [80001, 81658, 'Colorado'], 
        [82001, 83128, 'Wyoming'], [83201, 83876, 'Idaho'], [84001, 84784, 'Utah'], 
        [85001, 86556, 'Arizona'], [87001, 88441, 'New Mexico'], [88510, 88589, 'Texas'], 
        [88901, 89883, 'Nevada'], [90001, 96162, 'California'], [96701, 96898, 'Hawaii'], 
        [97001, 97920, 'Oregon'], [98001, 99403, 'Washington'], [99501, 99950, 'Alaska']
    ];

    while(i){
        next= zs[--i];
        if(z>next[0] && z<next[1]) return next[2];
    }
    return null;
}

<强> stateFromZip(&#39; 49125&#39)

/ *返回值:(String) 密歇根州 * /

答案 2 :(得分:2)

zippopotam.us有一个REST API。 以下是如何在纯JavaScript(无库)中从ZIP获取状态的示例:

var getStatebutton = document.getElementById('GetStateButton');

getStatebutton.onclick = function () {
    var zipCode = document.getElementById('ZIPCode');
    var zip = zipCode.value;
    if (!zip) return;
    var url = 'http://api.zippopotam.us/us/' + zip;
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            var result = xhr.responseText;
            var zippo = JSON.parse(result);
            var resultDiv = document.getElementById('divResult');
            resultDiv.innerHTML = zippo.places[0].state;
        }
    };
    xhr.open('GET', url, true);
    xhr.send(null);
};
ZIP:
<input type='text' id='ZIPCode' value='90210' />
<button id="GetStateButton">Get State</button>
<p></p>State:
<div id='divResult'></div>

jsfiddle如果您愿意。

答案 3 :(得分:0)

function getState(zipcode) {
    // Returns false on invalid zip-- else returns {code:"XX" long:"XXXXXXXXX"}

    // Ensure param is a string to prevent unpredictable parsing results
    if (typeof zipcode !== 'string') {
        console.log('Must pass the zipcode as a string. -- Otherwise leading zeros could cause your zip code to be parsed outside base 10.');
        return;
    }

    // Ensure you don't parse codes that start with 0 as octal values
    zipcode = parseInt(zipcode,10);

    // Code blocks alphabetized by state
    var states = [{min: 35000, max:36999, code: 'AL', long: "Alabama"},
    {min: 99500, max:99999, code: 'AK', long: "Alaska"},
    {min: 85000, max:86999, code: 'AZ', long: "Arizona"},
    {min: 71600, max:72999, code: 'AR', long: "Arkansas"},
    {min: 90000, max:96699, code: 'CA', long: "California"},
    {min: 80000, max:81999, code: 'CO', long: "Colorado"},
    {min: 6000,  max:6999,  code: 'CT', long: "Connecticut"},
    {min: 19700, max:19999, code: 'DE', long: "Deleware"},
    {min: 32000, max:34999, code: 'FL', long: "Florida"},
    {min: 30000, max:31999, code: 'GA', long: "Georgia"},
    {min: 96700, max:96999, code: 'HI', long: "Hawaii"},
    {min: 83200, max:83999, code: 'ID', long: "Idaho"},
    {min: 60000, max:62999, code: 'IL', long: "Illinois"},
    {min: 46000, max:47999, code: 'IN', long: "Indiana"},
    {min: 50000, max:52999, code: 'IA', long: "Iowa"},
    {min: 66000, max:67999, code: 'KS', long: "Kansas"},
    {min: 40000, max:42999, code: 'KY', long: "Kentucky"},
    {min: 70000, max:71599, code: 'LA', long: "Louisiana"},
    {min: 3900,  max:4999,  code: 'ME', long: "Maine"},
    {min: 20600, max:21999, code: 'MD', long: "Maryland"},
    {min: 1000,  max:2799,  code: 'MA', long: "Massachusetts"},
    {min: 48000, max:49999, code: 'MI', long: "Michigan"},
    {min: 55000, max:56999, code: 'MN', long: "Minnesota"},
    {min: 38600, max:39999, code: 'MS', long: "Mississippi"},
    {min: 63000, max:65999, code: 'MO', long: "Missouri"},
    {min: 59000, max:59999, code: 'MT', long: "Montana"},
    {min: 27000, max:28999, code: 'NC', long: "North Carolina"},
    {min: 58000, max:58999, code: 'ND', long: "North Dakota"},
    {min: 68000, max:69999, code: 'NE', long: "Nebraska"},
    {min: 88900, max:89999, code: 'NV', long: "Nevada"},
    {min: 3000, max:3899, code: 'NH', long: "New Hampshire"},
    {min: 7000, max:8999, code: 'NJ', long: "New Jersey"},
    {min: 87000, max:88499, code: 'NM', long: "New Mexico"},
    {min: 10000, max:14999, code: 'NY', long: "New York"},
    {min: 43000, max:45999, code: 'OH', long: "Ohio"},
    {min: 73000, max:74999, code: 'OK', long: "Oklahoma"},
    {min: 97000, max:97999, code: 'OR', long: "Oregon"},
    {min: 15000, max:19699, code: 'PA', long: "Pennsylvania"},
    {min: 300, max:999, code: 'PR', long: "Puerto Rico"},
    {min: 2800, max:2999, code: 'RI', long: "Rhode Island"},
    {min: 29000, max:29999, code: 'SC', long: "South Carolina"},
    {min: 57000, max:57999, code: 'SD', long: "South Dakota"},
    {min: 37000, max:38599, code: 'TN', long: "Tennessee"},
    {min: 75000, max:79999, code: 'TX', long: "Texas"},
    {min: 88500, max:88599, code: 'TX', long: "Texas"},
    {min: 84000, max:84999, code: 'UT', long: "Utah"},
    {min: 5000, max:5999, code: 'VT', long: "Vermont"},
    {min: 22000, max:24699, code: 'VA', long: "Virgina"},
    {min: 20000, max:20599, code: 'DC', long: "Washington DC"},
    {min: 98000, max:99499, code: 'WA', long: "Washington"},
    {min: 24700, max:26999, code: 'WV', long: "West Virginia"},
    {min: 53000, max:54999, code: 'WI', long: "Wisconsin"},
    {min: 82000, max:83199, code: 'WY', long: "Wyoming"}];

    var state = states.filter(function(s){
        return s.min <= zipcode && s.max >= zipcode;        
    });

    if (state.length == 0){
        return false;
    } else if (state.length > 1) {
        console.error("Whoops found two states");
    }
    return {code:state[0].code, long:state[0].long};

}

答案 4 :(得分:0)

我意识到OP最初是在要求PHP或JS,但是我到了这里,所以其他人也可能会觉得有用。

这是C#版本,具有更新的邮政编码范围(从42k邮政编码数据库构建)

public static string GetStateFromZip(int zipCode) {
    return zipCode switch {
        var zip when zip >= 600 && zip <= 999 => "Puerto Rico",
        var zip when zip >= 1001 && zip <= 2791 => "Massachusetts",
        var zip when zip >= 2801 && zip <= 2940 => "Rhode Island",
        var zip when zip >= 3031 && zip <= 3897 => "New Hampshire",
        var zip when zip >= 3901 && zip <= 4992 => "Maine",
        var zip when zip >= 5001 && zip <= 5495 => "Vermont",
        var zip when zip >= 5501 && zip <= 5544 => "Massachusetts",
        var zip when zip >= 5601 && zip <= 5907 => "Vermont",
        var zip when zip >= 6001 && zip <= 6389 => "Connecticut",
        var zip when zip >= 6390 && zip <= 6390 => "New York",
        var zip when zip >= 6401 && zip <= 6928 => "Connecticut",
        var zip when zip >= 7001 && zip <= 8989 => "New Jersey",
        var zip when zip >= 9000 && zip <= 9900 => "Military",
        var zip when zip >= 10001 && zip <= 14975 => "New York",
        var zip when zip >= 15001 && zip <= 19640 => "Pennsylvania",
        var zip when zip >= 19701 && zip <= 19980 => "Delaware",
        var zip when zip >= 20001 && zip <= 20039 => "Washington, D.C.",
        var zip when zip >= 20040 && zip <= 20167 => "Virginia",
        var zip when zip >= 20042 && zip <= 20599 => "Washington, D.C.",
        var zip when zip >= 20331 && zip <= 20331 => "Maryland",
        var zip when zip >= 20335 && zip <= 20797 => "Maryland",
        var zip when zip >= 20799 && zip <= 20799 => "Washington, D.C.",
        var zip when zip >= 20810 && zip <= 21930 => "Maryland",
        var zip when zip >= 22001 && zip <= 24658 => "Virginia",
        var zip when zip >= 24701 && zip <= 26886 => "West Virginia",
        var zip when zip >= 27006 && zip <= 28909 => "North Carolina",
        var zip when zip >= 29001 && zip <= 29948 => "South Carolina",
        var zip when zip >= 30001 && zip <= 31999 => "Georgia",
        var zip when zip >= 32000 && zip <= 34997 => "Florida",
        var zip when zip >= 35004 && zip <= 36925 => "Alabama",
        var zip when zip >= 37010 && zip <= 38589 => "Tennessee",
        var zip when zip >= 38601 && zip <= 39776 => "Mississippi",
        var zip when zip >= 39813 && zip <= 39897 => "Georgia",
        var zip when zip >= 39901 && zip <= 39901 => "Georgia",
        var zip when zip >= 40003 && zip <= 42788 => "Kentucky",
        var zip when zip >= 43001 && zip <= 45999 => "Ohio",
        var zip when zip >= 46001 && zip <= 47997 => "Indiana",
        var zip when zip >= 48001 && zip <= 49971 => "Michigan",
        var zip when zip >= 50001 && zip <= 52809 => "Iowa",
        var zip when zip >= 53001 && zip <= 54990 => "Wisconsin",
        var zip when zip >= 55001 && zip <= 56763 => "Minnesota",
        var zip when zip >= 56901 && zip <= 56999 => "Washington, D.C.",
        var zip when zip >= 57001 && zip <= 57799 => "South Dakota",
        var zip when zip >= 58001 && zip <= 58856 => "North Dakota",
        var zip when zip >= 59001 && zip <= 59937 => "Montana",
        var zip when zip >= 60001 && zip <= 62999 => "Illinois",
        var zip when zip >= 63001 && zip <= 65899 => "Missouri",
        var zip when zip >= 66002 && zip <= 67954 => "Kansas",
        var zip when zip >= 68001 && zip <= 68118 => "Nebraska",
        var zip when zip >= 68119 && zip <= 68120 => "Iowa",
        var zip when zip >= 68122 && zip <= 69367 => "Nebraska",
        var zip when zip >= 70001 && zip <= 71232 => "Louisiana",
        var zip when zip >= 71233 && zip <= 71233 => "Mississippi",
        var zip when zip >= 71234 && zip <= 71497 => "Louisiana",
        var zip when zip >= 71601 && zip <= 72959 => "Arkansas",
        var zip when zip >= 73001 && zip <= 73199 => "Oklahoma",
        var zip when zip >= 73301 && zip <= 73399 => "Texas",
        var zip when zip >= 73401 && zip <= 74966 => "Oklahoma",
        var zip when zip >= 75001 && zip <= 75501 => "Texas",
        var zip when zip >= 75502 && zip <= 75502 => "Arkansas",
        var zip when zip >= 75503 && zip <= 79999 => "Texas",
        var zip when zip >= 80001 && zip <= 81658 => "Colorado",
        var zip when zip >= 82001 && zip <= 83128 => "Wyoming",
        var zip when zip >= 83201 && zip <= 83876 => "Idaho",
        var zip when zip >= 84001 && zip <= 84790 => "Utah",
        var zip when zip >= 85001 && zip <= 86556 => "Arizona",
        var zip when zip >= 87001 && zip <= 88441 => "New Mexico",
        var zip when zip >= 88510 && zip <= 88595 => "Texas",
        var zip when zip >= 88901 && zip <= 89883 => "Nevada",
        var zip when zip >= 90001 && zip <= 96162 => "California",
        var zip when zip >= 96201 && zip <= 96698 => "Military",
        var zip when zip >= 96701 && zip <= 96898 => "Hawaii",
        var zip when zip >= 96910 && zip <= 96932 => "Guam",
        var zip when zip >= 97001 && zip <= 97920 => "Oregon",
        var zip when zip >= 98001 && zip <= 99403 => "Washington",
        var zip when zip >= 99501 && zip <= 99950 => "Alaska",
        _ => ""
    };
}