简体   繁体   English

使用纬度和经度iphone的地图上两点之间的距离方程

[英]Equation for distance between two points on the map using latitude and longitude iphone

I am trying to calculate the distance between two points (Pins) on the iPhone map. 我正在尝试计算iPhone地图上两个点(销)之间的距离。 Everything works fine however if you put one of the pin close to the left edge and the other one to the right edge of the map, distanceFromLocation method always returned the shortest distance between points (of course earth is not flat). 一切正常,但是,如果将其中一个图钉放置在地图的左边缘附近,而将另一个图钉放置在地图的右边缘,则distanceFromLocation方法始终返回点之间的最短距离(当然,地球不平坦)。 I tried switching the CLLocations but it still shows me the shortest distance. 我尝试切换CLLocations,但它仍然显示最短的距离。

For my app I will need to calculate both, shortest and the longest distance between two pins. 对于我的应用程序,我需要计算两个引脚之间的最短距离和最长距离。 The problem seems to be trivial but I can't come out with anything to solve it. 这个问题看似微不足道,但我无能为力。 Any help or clues appreciated. 任何帮助或线索表示赞赏。 Thanks! 谢谢!

There is a function MKCoordinateRegionForMapRect that returns a MKCoordinateRegion. 有一个函数MKCoordinateRegionForMapRect返回一个MKCoordinateRegion。 Without having tried it, seems like all you would need to do is accurately specify the rect as the region bounded at one diagonal by one pin and the other diagonal by the other pin. 无需尝试,似乎您需要做的就是准确地将rect指定为一个引脚在一个对角线处,另一引脚在另一个对角线处所界定的区域。 You could use CGRectUnion twice giving the pin origins as rect origin and an arbitrarily small width and height. 您可以使用CGRectUnion两次,将销钉的原点指定为rect原点,并任意指定宽度和高度。

Then the distance between the pins will be the square root of the distances represented by the latitudeDelta and longitudeDelta returned in the span squared and added. 然后,引脚之间的距离将是由跨度平方和相加得出的latitudeDelta和longitudeDelta表示的距离的平方根。 (ie Pythagoras theorem, the two side lengths are the horizontal and vertical sides of a triangle and the diagonal is a straight line between your points.) (即毕达哥拉斯定理,两条边的长度是三角形的水平和垂直边,对角线是您的点之间的直线。)

Finding latitudeDelta in metres is easy, each degree of latitude is 111km. 以米为单位查找纬度Delta很容易,每个纬度为111km。

To find meters for the longitudeDelta you need a little bit more code. 要查找经度Delta的米,您需要更多代码。

#define EARTH_EQUATORIAL_RADIUS (6378137.0)
#define WGS84_CONSTANT (0.99664719)

#define degreesToRadians(x) (M_PI * (x) / 180.0)

// accepts decimal degrees. Convert from HMS first if that's what you have
double lengthOfDegreeLongitude(double degrees) {

    double tanDegrees = tanf(degreesToRadians(degrees));
    double beta =  tanDegrees * WGS84_CONSTANT;
    double lengthOfDegree = cos(atan(beta)) * EARTH_EQUATORIAL_RADIUS * M_PI / 180.0;

    return lengthOfDegree;
}

Give that function the longitude of the location you are interested in and multiply the result by the longitudeDelta in degrees to get a distance in meters. 给该函数指定您感兴趣的位置的经度,然后将结果乘以度数经度Delta得出以米为单位的距离。 Then you have a horizontal distance and a vertical and you can find the diagonal which is what you were looking for. 然后,您有一个水平距离和一个垂直距离,并且可以找到想要的对角线。

Inaccuracy will get bad if you are looking for a distance which is very large in NS as the length of a degree longitude will be varying over the calculation. 如果您要寻找的距离在NS中非常大,则不准确度会变得很严重,因为度数的长度会随着计算而变化。 And you can refine the 111km for a degree of latitude a bit too, that's just from memory. 您也可以稍微调整一下111公里的纬度,这仅是从内存中得出的。

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

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