2011-08-03 16 views
1

これは、あるCLLocationから別のCLLocationへの角度(ラジアン単位)を計算する有効な方法ですか?2つのCLLocationsの間の角度を計算する有効な方法は?

-(float)angleFromLocation:(CLLocationCoordinate2D)start toLocation:(CLLocationCoordinate2D)end { 
float deltaX = start.latitude - end.latitude; 
float deltaY = start.longitude - end.longitude; 
float ang = atan2(deltaY, deltaX); 

return ang;} 

お助言ください!

ご協力いただければ幸いです。

+0

あなたが近づいています。この回答への解決策を見てください - http://stackoverflow.com/questions/6140045/how-to-get-degree-of-poi-from-another-poi – olo

答えて

0

私がこの計算で見つけた最良の方法は、球面法則を使うことでした。これを行うためのC関数があり、headingInDegreesというhere on githubが利用可能です。これは、見出し2緯度/経度のペアとリターンを取る:

/*------------------------------------------------------------------------- 
* Given two lat/lon points on earth, calculates the heading 
* from lat1/lon1 to lat2/lon2. 
* 
* lat/lon params in degrees 
* result in degrees 
*-------------------------------------------------------------------------*/ 
double headingInDegrees(double lat1, double lon1, double lat2, double lon2); 

CLLocationCoordinate2dは、緯度と経度が含まれているので、この関数に、これら二つのフィールドを通過することが容易であり、バック見出し得ます。

2

私はこのquestion and answerのバリアントを使用し、それがうまく機能:

double DegreesToRadians(double degrees) {return degrees * M_PI/180.0;}; 
double RadiansToDegrees(double radians) {return radians * 180.0/M_PI;}; 

- (double)bearingFromLocation:(CLLocation *)fromLocation toLocation:(CLLocation *)toLocation 
{ 

    double lat1 = DegreesToRadians(fromLocation.coordinate.latitude); 
    double lon1 = DegreesToRadians(fromLocation.coordinate.longitude); 

    double lat2 = DegreesToRadians(toLocation.coordinate.latitude); 
    double lon2 = DegreesToRadians(toLocation.coordinate.longitude); 

    double dLon = lon2 - lon1; 

    double y = sin(dLon) * cos(lat2); 
    double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon); 
    double radiansBearing = atan2(y, x); 

    double degreesBearing = RadiansToDegrees(radiansBearing); 

    if (degreesBearing >= 0) { 
     return degreesBearing; 
    } else { 
     return degreesBearing + 360.0; 
    } 
} 
+0

は完全に動作します!ありがとう! – coolbeet

+1

私は同様の問題があります。私はGPSの読みに基づいてユーザーの場所を追跡しています。 GPSの読みは私に「コース」の値を与えます。これは北に向かう方向を度で決めるものです。私が2つの座標の間の角度の差を計算すると、ユーザーが0から359度に移動すると物が醜くなります。リアルタイムでは、それは1度の変更でしたが、私のコードは359度の変化を与えています。何か案は? – Nil

0

スウィフト4バージョン:

extension FloatingPoint { 

    var degreesToRadians: Self { return self * .pi/180 } 
    var radiansToDegrees: Self { return self * 180/.pi } 
} 

extension CLLocationCoordinate2D: Equatable { 

    func heading(to: CLLocationCoordinate2D) -> Double { 
     let lat1 = self.latitude.degreesToRadians 
     let lon1 = self.longitude.degreesToRadians 

     let lat2 = to.latitude.degreesToRadians 
     let lon2 = to.longitude.degreesToRadians 

     let dLon = lon2 - lon1 
     let y = sin(dLon) * cos(lat2) 
     let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon) 

     let headingDegrees = atan2(y, x).radiansToDegrees 
     if headingDegrees >= 0 { 
      return headingDegrees 
     } else { 
      return headingDegrees + 360 
     } 
    } 
} 
関連する問題