[英]Distance between two geo-points?
How do I get an accurate distance (in meters) given two geo-points (two latitude/longitude pair) ? 如果给出两个地理点(两个纬度/经度对),我如何获得准确的距离(以米为单位)?
Possible Duplicates:
可能重复:
Distance Between Two GEO Locations
两个GEO位置之间的距离
Calculating the distance of geo locations
计算地理位置的距离
Android calculate distance between two locations
Android计算两个位置之间的距离
How to find distance from the latitude and longitude of two locations?
如何找到两个地点的纬度和经度的距离?
There's no distance measurement on the iPhone that will give you a resolution of 2 meters. iPhone上没有距离测量,可以提供2米的分辨率。 You can use Core Location's
-[CLLocation distanceFromLocation: otherLocation]
method to get a displacement in meters between two locations, but bear in mind: 您可以使用核心位置
-[CLLocation distanceFromLocation: otherLocation]
方法获取两个位置之间的米位移,但请记住:
If you want to get distance from two coordinates you can use this snippet: 如果您想从两个坐标获得距离,可以使用以下代码段:
#include <math.h>
#define DEG2RAD(degrees) (degrees * 0.01745327)
#define RADIUS_OF_EARTH 6378.1
+ (float) getDistanceFromStartCoords:(CLLocationCoordinate2D)start andEndCoords:(CLLocationCoordinate2D)end
{
float dist = acos((cos(DEG2RAD(start.latitude))*
cos(DEG2RAD(end.latitude))*
cos((-1*DEG2RAD(end.longitude))-
(-1*DEG2RAD(start.longitude)))) +
(sin(DEG2RAD(start.latitude))*
sin(DEG2RAD(end.latitude)))) *
RADIUS_OF_EARTH;
return dist;
}
This is an 'improvment' to the above solution. 这是对上述解决方案的“改进”。 It adds in altitude information.
它增加了高度信息。 It seems that the altitude that apple returns is in meters.
似乎苹果返回的海拔高度以米为单位。 Not suitable for flying or orbit or like that, but will work if someone is 15 floors directly above the other person, on a nearby mountain etc. Not extensively tested.
不适用于飞行或轨道或类似的情况,但如果有人在另一个人的正上方15层,附近的山上等,则不适用。未经过广泛测试。 It assumes that you don't care about altitude for something over 20km away.
它假设你不关心20公里以外的东西的高度。 It then feeds in an altitude correction as you are closer to the other person.
然后,当您离另一个人更近时,它会进行高度校正。 So for two people 20 metres away from each other, but 100m higher, you get a distance of about 102 meters.
因此对于距离彼此相距20米但距离高100米的两个人,你的距离约为102米。 Right at the end I switch to km for the return.
在最后,我切换到km返回。 Also found a nan bug in the original code.
还在原始代码中发现了一个nan bug。
#define DEG2RAD(degrees) (degrees * 0.01745329251)
#define RADIUS_OF_EARTH 6371000.0
// km
+ (double)getDistanceFromStartCoords:(CLLocationCoordinate2D)start altStart:(double)altStart andEndCoords:(CLLocationCoordinate2D)end altEnd:(double)altEnd;
{
double argument = (cos(DEG2RAD(start.latitude))*
cos(DEG2RAD(end.latitude))*
cos((-1*DEG2RAD(end.longitude))-
(-1*DEG2RAD(start.longitude)))) +
(sin(DEG2RAD(start.latitude))*
sin(DEG2RAD(end.latitude)));
double dist = 0.0;
if (argument < 1.0 && argument > -1.0) // acos will return nan for very small (0) distance
dist = acos(argument)*RADIUS_OF_EARTH;
// else
// NSLog(@"found bug, %f", acos(argument));
// Altitude hack.
// blend in an altitude correction (blend for smoothness)
// add in altitude difference
double altDiff = fabs(altStart - altEnd); // altdiff
double factor = 1.0 - dist/20000.0;
if (factor < 0.0)
factor = 0.0;
dist += sqrt(dist*dist + factor*altDiff*altDiff);
//NSLog(@"distance found, %f", dist);
return dist/1000.0; // return km
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.