6

私はいくつかのiPhoneアプリに取り組んでおり、加速度計を使用してユーザーの位置に応じて、特定の地理的位置に移動する画像であるベアリングを作成したいと考えています.

ここで多くの回答を読みましたが、解決策が得られませんでした。

現在の位置座標と目的地の座標があります。

アイデアやサンプルコードはありますか? ありがとう。

4

1 に答える 1

12

上にこれを定義する

#define RadiansToDegrees(radians)(radians * 180.0/M_PI)
#define DegreesToRadians(degrees)(degrees * M_PI / 180.0)

.hファイルで変数を定義する

float GeoAngle;

ロケーションマネージャーのデリゲートメソッドで:-

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    GeoAngle = [self setLatLonForDistanceAndAngle:newLocation];
}

そして、機能は次のようになります:-

-(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation
{
    float lat1 = DegreesToRadians(userlocation.coordinate.latitude);
    float lon1 = DegreesToRadians(userlocation.coordinate.longitude);

    float lat2 = DegreesToRadians(locLat);
    float lon2 = DegreesToRadians(locLon);

    float dLon = lon2 - lon1;

    float y = sin(dLon) * cos(lat2);
    float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
    float radiansBearing = atan2(y, x);
    if(radiansBearing < 0.0)
    {
        radiansBearing += 2*M_PI;
    }

    return radiansBearing;
}

GeoAngle変数で常に更新される角度を取得します。矢印を目的の場所に回転するには、矢印の画像を取得し、それをarrowImageViewのIBOutletに割り当て、見出しの更新メソッドでこれを回転させます。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{
    float direction = -newHeading.trueHeading;

    arrowImageView.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);
}
于 2012-05-23T12:04:09.460 に答える