質問

遠くのオブジェクトの方位角(コース)を知りたい(マップ上)。私はこの遠いオブジェクトの座標を持っています。私はこれをしようとしました:

CLLocation *distLoc = [CLLocation alloc] initWithLatitude:40.725405 
                                                longitude:-74.003906]; // NY city

NSLog(@"loc: %f", distLoc.course);

出力は次のとおりです。 loc: -1.0...

しかし、なぜ?

現在の方向からこのコースを知りたいです。また、ロケーションの更新もあります startUpdatingLocation メソッドとデリゲートの更新。

私は何が間違っていますか?

なぜ私は現在の位置から遠いオブジェクトのコースを取得できないのですか?ありがとう。

役に立ちましたか?

解決

- コースは、移動するオブジェクトの方向を返します。したがって、ユーザーの現在の場所で-Courseを呼び出します。それはあなたの場所から別の場所への道順をあなたに与えません。したがって、NYCが動いていない限り、それは常に-1を返します

コンパスの見出しを見つけたいなら、あなたはこれを行うことができます:

#define RAD_TO_DEG(r) ((r) * (180 / M_PI))

...    

CLLocationCoordinate2D coord1 = currentLocation.coordinate;
CLLocationCoordinate2D coord2 = distLoc.coordinate;

CLLocationDegrees deltaLong = coord2.longitude - coord1.longitude;
CLLocationDegrees yComponent = sin(deltaLong) * cos(coord2.latitude);
CLLocationDegrees xComponent = (cos(coord1.latitude) * sin(coord2.latitude)) - (sin(coord1.latitude) * cos(coord2.latitude) * cos(deltaLong));

CLLocationDegrees radians = atan2(yComponent, xComponent);
CLLocationDegrees degrees = RAD_TO_DEG(radians) + 360;

CLLocationDirection heading = fmod(degrees, 360);

他のヒント

以下のコードを使用してコースを取得します

CLLocation *distLoc =  [[CLLocation  alloc] initWithCoordinate:loc altitude:location.altitude horizontalAccuracy:location.horizontalAccuracy verticalAccuracy:location.verticalAccuracy  course:location.course speed:location.speed timestamp:location.timestamp];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top