This question already has an answer here:
这个问题已经有了答案:
- Distance between two coordinates with CoreLocation 4 answers
- 两个坐标之间的距离用无线电定位4个答案
i have latitude and longitude of particular place and i want to calculate the distance so how can i calculate it?
我有特定位置的纬度和经度我想计算距离我怎么计算呢?
3 个解决方案
#1
32
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(@"Distance i meters: %f", [location1 distanceFromLocation:location2]);
[location1 release];
[location2 release];
You also need to add CoreLocation.framework
to your project, and add the import statement:
您还需要在项目中添加coreloc.framework,并添加导入语句:
#import <CoreLocation/CoreLocation.h>
#2
5
This might not be the most efficient method of doing it, but it will work.
这可能不是最有效的方法,但它会起作用。
Your two locations specified by latitude and longitude can be considered vectors. Assuming that the coordinates have been converted into cartesion coordinates, calculate the dot product of the two vectors.
由纬度和经度指定的两个位置可以视为向量。假设坐标转换为笛卡尔坐标,计算这两个向量的点积。
Given v1 = (x1, y1, z1) and v2 = (x2, y2, z2), then ...
给定v1 = (x1, y1, z1) v2 = (x2, y2, z2),则…
v1 dot v2 = magnitude(v1) * magnitude(v2) * cos (theta)
Conveniently, the magnitude of v1 and v2 will be the same ... the radius of the earth (R).
很方便,v1和v2的大小是一样的。地球的半径(R)
x1*x2 + y1*y2 + z1*z2 = R*R*cos(theta)
Solve for theta.
解出θ。
theta = acos ((x1*x2 + y1*y2 + z1*z2) / (R * R));
Now you have angle between the two vectors in radians. The distance betwen the two points when travelling across the surface of earth is thus ...
现在两个向量之间的夹角是弧度。因此,在地球表面旅行时,两点之间的距离是……
distance = theta * R.
There is probably an easier way to do this entirely within the context of spherical coordinates, but my math in that area is too fuzzy--hence the conversion to cartesian coordinates.
也许有一种更简单的方法可以完全在球面坐标系下完成,但是我在那个区域的数学太模糊了——因此转换成笛卡尔坐标系。
To convert to cartesian coordinates ...
转换到笛卡尔坐标…
Let alpha be the latitude, and beta be the longitude.
设为纬度,设为经度。
x = R * cos (alpha) * cos (beta)
y = R * sin (alpha)
z = R * cos (alpha) * sin (beta)
Don't forget that the math function typically deal in radians, and the latitude/longitude deal in degrees.
不要忘记,数学函数通常用弧度表示,而纬度/经度则用度数表示。
#3
1
I've cranked through the math, and can now greatly simplify the solution.
我已经算过了,现在可以极大地简化解了。
Imagine if we spin the earth so that our first vector is at 0 degrees latitude and 0 degrees longitude. The second vector would be at (alpha2 - alpha1) degrees latitude and (beta2 - beta1) degrees latitude.
想象一下,如果我们旋转地球使第一个矢量在纬度0度,经度0度。第二个向量在(阿尔法2 -阿尔法1)纬度和(贝塔2 -贝塔1)纬度上。
Since ...
自……
sin(0) = 0 and cos(0) = 1
our dot product simplies to ...
我们的点产品simplies到…
cos(delta_alpha) * cos(delta_beta) = cos(theta)
The rest of the math remains unchanged.
剩下的数学没变。
theta = acos (cos(delta_alpha) * cos(delta_beta))
distance = radius * theta
Hope this helps.
希望这个有帮助。
#1
32
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
NSLog(@"Distance i meters: %f", [location1 distanceFromLocation:location2]);
[location1 release];
[location2 release];
You also need to add CoreLocation.framework
to your project, and add the import statement:
您还需要在项目中添加coreloc.framework,并添加导入语句:
#import <CoreLocation/CoreLocation.h>
#2
5
This might not be the most efficient method of doing it, but it will work.
这可能不是最有效的方法,但它会起作用。
Your two locations specified by latitude and longitude can be considered vectors. Assuming that the coordinates have been converted into cartesion coordinates, calculate the dot product of the two vectors.
由纬度和经度指定的两个位置可以视为向量。假设坐标转换为笛卡尔坐标,计算这两个向量的点积。
Given v1 = (x1, y1, z1) and v2 = (x2, y2, z2), then ...
给定v1 = (x1, y1, z1) v2 = (x2, y2, z2),则…
v1 dot v2 = magnitude(v1) * magnitude(v2) * cos (theta)
Conveniently, the magnitude of v1 and v2 will be the same ... the radius of the earth (R).
很方便,v1和v2的大小是一样的。地球的半径(R)
x1*x2 + y1*y2 + z1*z2 = R*R*cos(theta)
Solve for theta.
解出θ。
theta = acos ((x1*x2 + y1*y2 + z1*z2) / (R * R));
Now you have angle between the two vectors in radians. The distance betwen the two points when travelling across the surface of earth is thus ...
现在两个向量之间的夹角是弧度。因此,在地球表面旅行时,两点之间的距离是……
distance = theta * R.
There is probably an easier way to do this entirely within the context of spherical coordinates, but my math in that area is too fuzzy--hence the conversion to cartesian coordinates.
也许有一种更简单的方法可以完全在球面坐标系下完成,但是我在那个区域的数学太模糊了——因此转换成笛卡尔坐标系。
To convert to cartesian coordinates ...
转换到笛卡尔坐标…
Let alpha be the latitude, and beta be the longitude.
设为纬度,设为经度。
x = R * cos (alpha) * cos (beta)
y = R * sin (alpha)
z = R * cos (alpha) * sin (beta)
Don't forget that the math function typically deal in radians, and the latitude/longitude deal in degrees.
不要忘记,数学函数通常用弧度表示,而纬度/经度则用度数表示。
#3
1
I've cranked through the math, and can now greatly simplify the solution.
我已经算过了,现在可以极大地简化解了。
Imagine if we spin the earth so that our first vector is at 0 degrees latitude and 0 degrees longitude. The second vector would be at (alpha2 - alpha1) degrees latitude and (beta2 - beta1) degrees latitude.
想象一下,如果我们旋转地球使第一个矢量在纬度0度,经度0度。第二个向量在(阿尔法2 -阿尔法1)纬度和(贝塔2 -贝塔1)纬度上。
Since ...
自……
sin(0) = 0 and cos(0) = 1
our dot product simplies to ...
我们的点产品simplies到…
cos(delta_alpha) * cos(delta_beta) = cos(theta)
The rest of the math remains unchanged.
剩下的数学没变。
theta = acos (cos(delta_alpha) * cos(delta_beta))
distance = radius * theta
Hope this helps.
希望这个有帮助。