从经度/纬度获取邮政编码 [英] Get zip code from latitude/longitude
问题描述
我有一个控件,允许用户输入邮政编码或在iframe中打开Google地图,然后单击地图以选择位置.我目前可以正常使用,iframe会将点击事件中的经纬度发送回父级.
I have a control that allows a user to either enter a zip code or open up google maps in an iframe and click on the map to pick a location. I currently have this all working and the iframe will send the lat/lon from the click event back to the parent.
我还有一个数据库表,其中设置了经/纬度字段映射到邮政编码.
I also have a db table set up with lat/lon fields mapped to zip codes.
但是,经/纬度组合很可能永远不会与数据库中的条目完全匹配.因此,我正在寻找匹配最接近的邮政编码的最佳方法.
However, the lat/lon combination will most likely never exactly match the entry I have in the db. So, I'm looking for the best way to match the closest zip code.
因为用户可以手动输入邮政编码,所以我并不完全担心精度(即,如果用户单击邮政编码之间的边界),但是我希望它尽可能地靠近.我现在只需要担心在美国的位置.
Because the user has the ability to manually enter the zip code, I'm not totally worried about precision(i.e. if a user clicks on a border between zip codes), but I would like it to be close as possible. I only need to worry about US locations for now.
我目前的解决方案是提出一些公差(.2?)并向db查询纬度/经度+-公差.然后从结果集中选择最接近的匹配项.
My current idea for a solution is to come up with some tolerance(.2?) and query the db for the lat/long +- the tolerance. Then pick the closest match from the result set.
我很想听听以前做过此事的人或有比我建议的解决方案更好的解决方案的人.我对使用服务没有兴趣,因为我拥有所需的所有数据.
I'd love to hear from someone who has done this before or has a better solution than the one I proposed. I'm not interested in using a service as I have all the data I need.
我对我提出的解决方案的一个担心是弄清楚什么是好的公差.该项目是针对农村地区的,我不确定这些地区的经/纬度条目分布得如何.
One concern I have with my proposed solution is figuring out what a good tolerance would be. This project is targeted for rural areas and I'm not sure how spread out the lat/lon entries for these areas could be.
这是我的表结构: zip5 |城市|州| lat | lon |县
Here is my table structure: zip5 | city | state | lat | lon | county
zip5是主键
推荐答案
此问题的规范解决方案使用R树.
The canonical solution to this problem uses R-trees.
这篇关于从经度/纬度获取邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!