简体   繁体   English

计算位置与位置列表之间的距离

[英]calculate distance between a location and a list of locations

I have a list of locations , following this pattern: 我有一个位置列表,遵循这种模式:

[1] = {lat = -40.2452, longitude = -76.2489},
[2] = {lat = -40.2452, longitude = -76.2489},
[3] = {lat = -40.2452, longitude = -76.2489},
[4] = {lat = -40.2452, longitude = -76.2489}

and a localition 和一个地区

location = {lat = -40.2452, longitude = -76.2489}

and I want to calculate Which of the locations this Within the distance. 我想计算该距离内的哪个位置。

I found a formula that calculates the distance between two points. 我找到了一个计算两点之间距离的公式。

but if this list is big! 但是如果这个名单很大!

is there any faster way? 有没有更快的方法?

or you can go through the list in a loop ? 或者您可以循环浏览列表?

FOR LOCATION IN LISTLOCATION DO
    IF GETDISTANCE(LOCATION, LOCATION2) <= DISTANCE
        SAVE THIS LOCATION
    END
END

These values are a example 这些值是一个例子

Basically you have to check each and every point, no other option. 基本上,您必须检查每个点,没有其他选择。 The distance formula (Haversine) is indeed slow, since it uses few trigonometric functions. 距离公式(Haversine)确实很慢,因为它使用的三角函数很少。 What you actually want is to plot a circle around your point, whose radius R is the distance, and check for each point if it's inside that circle: 您真正想要的是在您的点周围绘制一个圆,其半径R为距离,并检查每个点是否在该圆内:
在此处输入图片说明

The problem is that your point are given as (lat, long) pair, not (x,y) pair, so you can't use "regular" trigonometric methods, like the circle's equation. 问题在于您的点是(纬,长)对,而不是(x,y)对,因此您不能使用“正则”三角法,例如圆的方程。
Instead, you have to find a square that bounds that circle. 相反,您必须找到一个以该圆为界的正方形。 Go 90 degrees to the north and south, find the upper and lower longitudes of that square. 向南和北90度旋转,找到该正方形的上下经度。 Do the same to rhe east and west, find the upper and lower latitudes: 对东和西进行相同操作,找到上下纬度:
在此处输入图片说明

Now you can check for each point if it's inside the box, and you can do it easily with simple comparsions: 现在,您可以检查每个点是否在盒子内,并且可以通过简单的比较轻松地做到:

if lon > lon1 and lon < lon2
   and lat > lat2 and lat < lat1

which is really computionally cheap. 这真的很便宜。
The only problem is with points that are inside the blue area: 唯一的问题是蓝色区域内的点:
在此处输入图片说明

They are inside the square but not inside the circle, so you'll have to use the Haversine formula for them. 它们在正方形内,但不在圆内,因此您必须使用Haversine公式。
If most of your points are not in the square, this method will save you time, because eliminating them is pretty easy. 如果您的大多数点不在正方形中,则此方法将节省您的时间,因为消除它们非常容易。

If you are on Android then you can use the "distanceBetween" method of the Location class . 如果您使用的是Android,则可以使用Location类“ distanceBetween”方法 This method has it's implementation in Java (you can do a loop to test the distance between your location and the location list elements), so if you want higher performance you should do it natively (NDK). 此方法具有Java的实现(您可以执行循环以测试位置与位置列表元素之间的距离),因此,如果要获得更高的性能,则应本机执行(NDK)。 If you do it natevely you should pass the complete list to the native method and not one by one because the context change between the virtual and the native environment can be costly. 如果您精打细算,则应将完整列表传递给本机方法,而不要一一传递,因为虚拟环境和本机环境之间的上下文更改可能会很昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM