get 2 letter state/province abbreviation

时间秒杀一切 提交于 2019-12-10 14:01:39

问题


So I'm using the GeoNames API to get country and state/province information which I'm using to populate select dropdowns in a form. This form submits it's information to a SOAP web service and the SOAP Server only understands the country and state/province data in 2 letter form. ie. CA for Canada, US for United States. The GeoNames API provides this information for the different countries but not for their children (states/provinces).

Does anyone know how to get the 2 letter abbreviation for a state/province/territory from GeoNames or is there another API I can try?


回答1:


I'm not familiar with the GeoNames API, but it appears you are getting the ISO-3166 alpha-2 country codes: http://www.iso.org/iso/country_codes.htm

If so, ISO 3166-2:2007 defines states and provinces. The page above describes how to purchase the information in database and other formats.

Unless its a requirement to get this information from a web service, I would think it's simpler to read the dropdown list from some type of static file, and create a mechanism for occasional updates. This type of data shouldn't change that often (well, in stable countries at least).




回答2:


In agreement with @user640118, state abbreviations don't change often, if ever, in North America. It would be best to just have a static listing of codes or abbreviations.

To answer your question, though, I've worked on one such web service called LiveAddress which provides geo/postal data via a simple REST endpoint. It's a bit more advanced, though, in the sense that it accepts an address, partial or complete, and fills it out completely and verifies it and standardizes it. You may find that to be valuable in removing duplicates and geocoding your users' data.

If you have an address, you can use this wrapper function like so, in Javascript:

LiveAddress.verify("123 main st 12345", function(results) {
   console.log(results);
});

Sample output can be found in the API docs.




回答3:


Geonames provides 2 letter abbreviations for countries but only has them for states in a few countries.

Why they don't provide something as standard (and useful) as this is beyond me.



来源:https://stackoverflow.com/questions/11318203/get-2-letter-state-province-abbreviation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!