3

アプリに問題が1つあります。現在の緯度と経度から緯度と経度への方向を知りたいです。

-(void)showDirection
{
 CGFloat latitude = Lat;
 if (latitude < 0) {
    latitude = -latitude;
    strDirection = @"S";
 } else {
    strDirection = @"N";
 }
 // Longitude
 CGFloat longitude = Long;
 if (longitude < 0) {
    longitude = -longitude;
    strDirection = @"W";
 } else {
    strDirection = @"E";
 }
 NSLog(@"direc %@",strDirection);
}
4

2 に答える 2

8

以下のコードを試してください。

初期化CLLocationManagerオブジェクトを追加し、CLLocationManagerヘッダーファイルをインポートした後、最初にプロジェクトCoreLocation.Frameworkにフレームワークを追加します。

Yourviewcontroller.h

#import <CoreLocation/CoreLocation.h>
@interface Yourviewcontroller :UIViewController <CLLocationManagerDelegate>
{
      CLLocationManager *locationManager;
} 

Yourviewcontroller.m

locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
//Start the compass updates.
[locationManager startUpdatingHeading];

現在の方向を取得するための関数を追加します

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    float mHeading = newHeading.magneticHeading;
    if ((mHeading >= 339) || (mHeading <= 22)) {
        //[direction setText:@"N"]; 

    }else if ((mHeading > 23) && (mHeading <= 68)) {
        //[direction setText:@"NE"];    

    }else if ((mHeading > 69) && (mHeading <= 113)) {
        //[direction setText:@"E"]; 

    }else if ((mHeading > 114) && (mHeading <= 158)) {
        //[direction setText:@"SE"];

    }else if ((mHeading > 159) && (mHeading <= 203)) {
        //[direction setText:@"S"]; 

    }else if ((mHeading > 204) && (mHeading <= 248)) {
        //[direction setText:@"SW"];    

    }else if ((mHeading > 249) && (mHeading <= 293)) {
       // [direction setText:@"W"];

    }else if ((mHeading > 294) && (mHeading <= 338)) {
       // [direction setText:@"NW"];

     }
}

注:コンパスは実際のiphoneデバイスでのみ機能し、iphoneシミュレーターでは機能しません。

于 2012-04-20T11:47:34.597 に答える
-4

LocateMeを試すことができます。それはあなたにユーザーの現在の位置の長いと緯度を与えるでしょう。または、このチュートリアル
を見てみることができます

お役に立てれば!

于 2012-04-20T11:48:57.370 に答える