检测某个纬度/经度位于美国某个州
我知道大多数人会看到这个问题并向我指出 Google 地理编码 - 但我正在寻找一个数学公式,允许某人获取纬度/经度点并查看它是否在美国州(或边界框)内。有没有一种方法可以通过 PHP 进行计算,看看某个点是否在某个方框中(例如加利福尼亚州)?
I know that most people will view this question and point me to Google Geocode - but I'm looking for a mathematical formula that allows someone to take a Lat/Lng point and see if its inside a US state (or a bounding box). Is there a way via PHP, that I can do a calculation to see if a point is in a certain Box (such as California)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,没有任何公式可以告诉您有关哪些州在哪里的任何信息(如果有的话,那将完全破坏美墨战争的结果!)因此您需要从某个地方获取这些数据。
然后,这会变成两个问题之一,具体取决于您想要的准确程度。
如果您有在墨卡托投影或类似投影上显示的矩形边界框的详细信息(即,它具有北和南的纬度,以及东和西的经度),那么公式很简单:
如果您具有更多细节,并且具有一系列定义状态边界的点(显然,点越多,精度越高),那么它就成为多边形内点问题的变体,并保证只涉及简单的多边形(美国没有一个州有跨越自身的边界,也没有完全包围 此 C 代码。由于这是一种 2D 平面算法而不是球形算法,因此可能会出现边缘情况,但我想您需要一些非常精确的数据算法不精确性的状态边界大于数据引起的不精确性。
Well, there's no formula that'll tell you anything about what states is where (it would have totally been a spoiler as to the outcome of the US-Mexico war if there was!) So you'll need to get that data from somewhere.
This then turns into one of two problems, depending on the degree of accuracy you want.
If you have details of a bounding box that is rectangular when shown on a Mercator or similar projection (that is, it has degrees of latitude for north and south, and of longitude for east and west), then the formula is simply:
If you have more detail, and have a series of points that defines the border of the state (obviously, the more points, the more precision) then it becomes a variant of the point-in-polygon problem, with a guarantee of only involving simple polygons (no US state has a border that crosses itself, nor completely surrounds that used in this C code. It's possible that there would be edge cases affected by the fact that this is a 2D-plane algorithm rather than a spherical one, but I imagine you'd need to have some pretty precise data on the boundaries of the states for the imprecision from the algorithm to be greater than that caused by the data.
我认为最简单的方法是为每个状态使用绑定框,可以从 Flicker Geo API 找到,CA 的示例 - https://www.flickr.com/places/info/2347563
The simplest way I would think is using bound box for each state, that can be found from Flicker Geo API, an example for CA- https://www.flickr.com/places/info/2347563