Ton*_*nas 3 javascript php zipcode geo
我不需要城市或地址,只需要州.如果可能的话,我不想进行API调用.优先考虑的是轻量级解决方案,理想情况下只需使用Javascript.
我有一个用户输入zipcode,我想显示具体取决于该邮政编码所处的状态一段文字.我知道这复杂得多查找城市和国家,并为一个API,如一个USPS自曝可能是最好的.但是为了匹配状态,也许仅仅是前三个数字,解决方案(我认为)应该简单轻便.
理想的Javascript.PHP也可以工作.
Ton*_*nas 14
美国邮政编码数据实际上足够稳定,如果只需要状态(不是城市或其他任何东西),您可以在不点击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 we have exactly 5 characters to parse
if (zipcode.length !== 5) {
console.log('Must pass a 5-digit zipcode.');
return;
}
// Ensure we don't parse strings starting with 0 as octal values
const thiszip = parseInt(zipcode, 10);
const st;
const state;
// Code blocks alphabetized by state
if (thiszip >= 35000 && thiszip <= 36999) {
st = 'AL';
state = 'Alabama';
}
else if (thiszip >= 99500 && thiszip <= 99999) {
st = 'AK';
state = 'Alaska';
}
else if (thiszip >= 85000 && thiszip <= 86999) {
st = 'AZ';
state = 'Arizona';
}
else if (thiszip >= 71600 && thiszip <= 72999) {
st = 'AR';
state = 'Arkansas';
}
else if (thiszip >= 90000 && thiszip <= 96699) {
st = 'CA';
state = 'California';
}
else if (thiszip >= 80000 && thiszip <= 81999) {
st = 'CO';
state = 'Colorado';
}
else if (thiszip >= 6000 && thiszip <= 6999) {
st = 'CT';
state = 'Connecticut';
}
else if (thiszip >= 19700 && thiszip <= 19999) {
st = 'DE';
state = 'Delaware';
}
else if (thiszip >= 32000 && thiszip <= 34999) {
st = 'FL';
state = 'Florida';
}
else if (thiszip >= 30000 && thiszip <= 31999) {
st = 'GA';
state = 'Georgia';
}
else if (thiszip >= 96700 && thiszip <= 96999) {
st = 'HI';
state = 'Hawaii';
}
else if (thiszip >= 83200 && thiszip <= 83999) {
st = 'ID';
state = 'Idaho';
}
else if (thiszip >= 60000 && thiszip <= 62999) {
st = 'IL';
state = 'Illinois';
}
else if (thiszip >= 46000 && thiszip <= 47999) {
st = 'IN';
state = 'Indiana';
}
else if (thiszip >= 50000 && thiszip <= 52999) {
st = 'IA';
state = 'Iowa';
}
else if (thiszip >= 66000 && thiszip <= 67999) {
st = 'KS';
state = 'Kansas';
}
else if (thiszip >= 40000 && thiszip <= 42999) {
st = 'KY';
state = 'Kentucky';
}
else if (thiszip >= 70000 && thiszip <= 71599) {
st = 'LA';
state = 'Louisiana';
}
else if (thiszip >= 3900 && thiszip <= 4999) {
st = 'ME';
state = 'Maine';
}
else if (thiszip >= 20600 && thiszip <= 21999) {
st = 'MD';
state = 'Maryland';
}
else if (thiszip >= 1000 && thiszip <= 2799) {
st = 'MA';
state = 'Massachusetts';
}
else if (thiszip >= 48000 && thiszip <= 49999) {
st = 'MI';
state = 'Michigan';
}
else if (thiszip >= 55000 && thiszip <= 56999) {
st = 'MN';
state = 'Minnesota';
}
else if (thiszip >= 38600 && thiszip <= 39999) {
st = 'MS';
state = 'Mississippi';
}
else if (thiszip >= 63000 && thiszip <= 65999) {
st = 'MO';
state = 'Missouri';
}
else if (thiszip >= 59000 && thiszip <= 59999) {
st = 'MT';
state = 'Montana';
}
else if (thiszip >= 27000 && thiszip <= 28999) {
st = 'NC';
state = 'North Carolina';
}
else if (thiszip >= 58000 && thiszip <= 58999) {
st = 'ND';
state = 'North Dakota';
}
else if (thiszip >= 68000 && thiszip <= 69999) {
st = 'NE';
state = 'Nebraska';
}
else if (thiszip >= 88900 && thiszip <= 89999) {
st = 'NV';
state = 'Nevada';
}
else if (thiszip >= 3000 && thiszip <= 3899) {
st = 'NH';
state = 'New Hampshire';
}
else if (thiszip >= 7000 && thiszip <= 8999) {
st = 'NJ';
state = 'New Jersey';
}
else if (thiszip >= 87000 && thiszip <= 88499) {
st = 'NM';
state = 'New Mexico';
}
else if (thiszip >= 10000 && thiszip <= 14999) {
st = 'NY';
state = 'New York';
}
else if (thiszip >= 43000 && thiszip <= 45999) {
st = 'OH';
state = 'Ohio';
}
else if (thiszip >= 73000 && thiszip <= 74999) {
st = 'OK';
state = 'Oklahoma';
}
else if (thiszip >= 97000 && thiszip <= 97999) {
st = 'OR';
state = 'Oregon';
}
else if (thiszip >= 15000 && thiszip <= 19699) {
st = 'PA';
state = 'Pennsylvania';
}
else if (thiszip >= 300 && thiszip <= 999) {
st = 'PR';
state = 'Puerto Rico';
}
else if (thiszip >= 2800 && thiszip <= 2999) {
st = 'RI';
state = 'Rhode Island';
}
else if (thiszip >= 29000 && thiszip <= 29999) {
st = 'SC';
state = 'South Carolina';
}
else if (thiszip >= 57000 && thiszip <= 57999) {
st = 'SD';
state = 'South Dakota';
}
else if (thiszip >= 37000 && thiszip <= 38599) {
st = 'TN';
state = 'Tennessee';
}
else if ( (thiszip >= 75000 && thiszip <= 79999) || (thiszip >= 88500 && thiszip <= 88599) ) {
st = 'TX';
state = 'Texas';
}
else if (thiszip >= 84000 && thiszip <= 84999) {
st = 'UT';
state = 'Utah';
}
else if (thiszip >= 5000 && thiszip <= 5999) {
st = 'VT';
state = 'Vermont';
}
else if (thiszip >= 22000 && thiszip <= 24699) {
st = 'VA';
state = 'Virgina';
}
else if (thiszip >= 20000 && thiszip <= 20599) {
st = 'DC';
state = 'Washington DC';
}
else if (thiszip >= 98000 && thiszip <= 99499) {
st = 'WA';
state = 'Washington';
}
else if (thiszip >= 24700 && thiszip <= 26999) {
st = 'WV';
state = 'West Virginia';
}
else if (thiszip >= 53000 && thiszip <= 54999) {
st = 'WI';
state = 'Wisconsin';
}
else if (thiszip >= 82000 && thiszip <= 83199) {
st = 'WY';
state = 'Wyoming';
}
else {
st = 'none';
state = 'none';
}
return st;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢来自@凯文-鲍彻和@ abaldwin99帮助上解析小新英格兰代码,避免与他们的答案可怕的八进制评估错误在这里.
还要感谢大部分原始代码转到这个有用的页面.
小智 6
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};
}
Run Code Online (Sandbox Code Playgroud)
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);
};Run Code Online (Sandbox Code Playgroud)
ZIP:
<input type='text' id='ZIPCode' value='90210' />
<button id="GetStateButton">Get State</button>
<p></p>State:
<div id='divResult'></div>Run Code Online (Sandbox Code Playgroud)
或者如果您愿意,可以在jsfiddle 中使用。
小智 5
我最终从美国邮政编码数据库下载了一个 CSV 文件。我也不想与依赖 API 集成。
\nCSV 文件包含的信息超出了我的需要。我只解析了文件的 zip 和 state 列,并创建了一个邮政编码的哈希值来表示我在应用程序中维护和使用的状态代码(我正在使用 Ruby on Rails)。你可以用 Javascript 做类似的事情。
\n当前对使用范围的接受答案的修订存在问题。
\n首先,这个答案没有考虑到美国边远地区,例如 AE(欧洲武装部队https://www.zip-codes.com/state/ae.asp)、AA(美洲武装部队https://www.zip -codes.com/state/aa.asp),以及其他。
\n其次,范围发生变化,邮政编码也发生变化(尽管不经常发生)。所接受的答案进行了多次修订,以将“边缘情况”和“角落情况”添加到多年来的范围中。但我认为这里真正发生的是邮政编码正在变化,并且该答案中的范围必须不断更新。我发现几个邮政编码不适用于已接受答案的当前版本。可能还有更多示例 \xe2\x80\x94 我没有进行详尽的检查。[小提琴]
\n// Prints DC, but should be MD\n// https://www.zip-codes.com/zip-code/20588/zip-code-20588.asp\nconsole.log(`20588: ${getState("20588")}`);\n\n// Prints OK, but should be TX\n// https://www.zip-codes.com/zip-code/73960/zip-code-73960.asp\nconsole.log(`73960: ${getState("73960")}`);\n\n// Prints ID, but should be WY\n// https://www.zip-codes.com/zip-code/83414/zip-code-83414.asp\nconsole.log(`83414: ${getState("83414")}`);\n\n// Prints OK, but should be TX\n// https://www.zip-codes.com/zip-code/73960/zip-code-73960.asp\nconsole.log(`73960: ${getState("73960")}`);\nRun Code Online (Sandbox Code Playgroud)\n确实,从美国邮政编码数据库的 CSV 文件下载仅代表邮政编码数据的一个时间点。使用此解决方案的任何人都需要定期重新下载该文件以获得最新数据。但考虑到他们网站上的保证,最好依赖这个维护的事实来源,而不是依赖范围。
\n从他们的网站:
\n\n\n“我们不断监控 USPS 数据的重大变化,并在出现重大变化时发布新数据。我们信心十足,因此我们提供退款保证:如果您在我们进行数据更新之前发现任何重大变化,我们将发布全额退款。”
\n