找到两个位置之间的角度

时间:2013-03-28 12:20:43

标签: iphone ios cllocationmanager angle

我有两个位置的纬度和经度。

我已经找到了这两个位置之间的距离。

CLLocation *locA = [[CLLocation alloc] initWithLatitude:[player.strLatitude floatValue] longitude:[player.strLongitude floatValue]];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:app.lat longitude:app.lag];
CLLocationDistance distance = [locB distanceFromLocation:locA];
NSLog(@"Distance%f",distance);
[locA release]; [locB release];

现在我想找出这两个位置之间的角度。

在中心有一个用户位置,我想以适当的角度显示该Cicle中的其他用户位置。

感谢您的帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

也许这会对你有所帮助

- (CGFloat)angleBetweenLinesInRadians:(CGPoint)line1Start 
                         line1End:(CGPoint)line1End 
                       line2Start:(CGPoint)line2Start 
                         line2End:(CGPoint)line2End 
{
CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;

CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);

CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));


return (line2Slope > line1Slope) ? degs : -degs;    

}