查找给定邮政编码的最近邮政编码
在我的 php 应用程序中,我想获取给定邮政编码最近的邮政编码。 这意味着我输入的邮政编码为 680721
我想从我的数据库中获取最近的邮政编码。
我该怎么做?
这是我用于存储邮政编码的表格。
这里 varpin
是邮政编码字段。
In my php application I want to get the nearest postal code of the given post code.
That means I enter a post code as 680721
I want to get the nearest post code of this from my database.
How can I do this?
This is the table I used for store postal codes.
Here varpin
is the postal code field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说了这么多,快速浏览英国邮政编码维基百科条目上的“外部链接”,我很快找到了 Paul Jenkins 的一篇文章,标题为 PHP 中的英国邮政编码距离计算,这篇文章非常棒,您甚至可以在这里下载( uk_postcode_calc.zip)。
经过简短的检查后,这似乎完全按照罐子上的说明进行操作,只是计算距离。
然而,通过快速谷歌搜索 php 距离计算,您可以很快发现距离计算有更精细的等价物。我认为使用其中之一可能是个好主意。
经过一番调整后,这就是我最终得出的结论:
所以,这就是完成的困难部分(数据库和数学),接下来就是使用这些信息从我们输入的邮政编码“找到最接近的”的情况我们提供的一系列邮政编码...
为了找到“最近”的邮政编码,实际上我们要做的是找到邮政编码之间的“最短”距离,或者只是结果中的最小数字,假设我们将结果放入一个以键为键的数组中邮政编码和距离作为值。
我们所要做的就是创建一个简单的脚本,它将找到给定数组中的最小数字,然后返回适当的键。简单的!
上面的脚本正是我们想要的,使用“min”函数我们可以快速计算出我们需要返回的内容。
剩下的唯一任务是将所有这些绑定在一起,我们需要创建两个函数:
使用邮政编码从数据库中获取经度和纬度来获取距离。
创建一个数组,其中邮政编码作为键,距离作为值。
很简单!
函数 1,邮政编码距离
因此,完成后,将上述 4 个函数放入一个文件中,例如“postcode.php”,准备在现实世界中使用......
测试用例:
您可以在此处下载此脚本:postcode_search.phps
注意:在上面的测试用例中,我有一个“db.php”文件,其中包含我的数据库详细信息并启动数据库连接。我建议你也这样做。
确保您已填充数据库,您应该能够使用 Paul Jenkins 的英国邮政编码 csv,从而允许您使用自己的表结构。
好了,这就是大家,我现在可以使用此脚本来提供与“最近”邮政编码匹配的任何位置。
Having said all this, a quick browse through the “External Links” on the UK Postcodes Wikipedia entry, and I quickly found an article by Paul Jenkins entitled UK Post Code Distance Calculation in PHP, which is fantastic, you can even download it here (uk_postcode_calc.zip).
After a short examination it seems this does exactly what it says on the tin, and simply calculates the distance.
However, with a quick google for php distance calculation, you can quickly find that there are more refined equivalents of the distance calculation. I thought it might be a good idea to use one of those instead.
After a bit of tweaking, here’s what I came up with in the end:
So, that’s the hard parts done (database and maths), next is simply a case of using this information to “find the closest” from the postcode we input to an array of postcodes we supply…
To find the “closest” postcode, effectively what we’re trying to do is find the “shortest” distance between the postcodes, or, simply the smallest number in the results, assuming we put the results into an array with the key as the postcode and the distance as the value.
All we have to do is create a simple script that will find the smallest number in a given array, then return the appropriate key. Simple!
The above script does exactly what we want, using the “min” function we can quickly work out what we need to return.
The only task left is to bind all this together, we need to create two functions that will:
Get the distance using the postcode to get the longitude and latitude from the database.
Create an array with the postcodes as the keys, and the distance as the values.
Very simple!
Function 1, Postcode Distance
So, with that done, place the 4 above functions into a file such as “postcode.php”, ready for use in the real world…
Test case:
You can download this script here: postcode_search.phps
Note: In the above test case, I have a “db.php” file which contains my database details and starts a database connection. I suggest you do the same.
Ensure you have your database populated, you should be able to use Paul Jenkins’s UK Postcode csv, allowing you to use your own table structure.
Well, that’s all folks, I can now use this script to provide any locations that match the “closest” postcode.