0

次のコードを使用して、アプリの複数の部分でユーザーの場所を見つけたいと考えています。複雑さを避けるために、ユーザーの場所を取得する場所を 1 つだけにしたいと考えています。それを appDelegate に入れ、それを使用する必要があるときに他のビューから何らかの方法で呼び出す必要があると思います。私のappDelegate以外のビュー/タブで場所をNSStringとして取得するために何を使用する必要があるか知っている人はいますか? ありがとうございました!

- (NSString *)deviceLocation {
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", 
locationManager.location.coordinate.latitude, 
locationManager.location.coordinate.longitude];
return theLocation;
}

- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
4

2 に答える 2

1

すべてのロケーション リスナーと関連するものをアプリ デリゲートに移動し、 のNSString *location プロパティを作成しますAppDelegate。更新されたロケーション セットを取得するたびにNSString *location、他のViewControllersで、必要に応じてアプリ デリゲートのロケーション プロパティにアクセスします。

更新された場所が本当に必要な場合は、NSNotificationCenterを使用して、新しい場所の更新に関する通知をブロードキャストできます。そのため、ViewController更新された場所が本当に必要な は、新しい場所に関する通知に自身を登録し、それに応じて処理を行います。

于 2012-12-15T22:58:25.197 に答える
1
You can put your code in app delegate..like this

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
@public
    float latitude;
    float longituted;
}


@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    if (locationManager==nil) {
        locationManager=[[CLLocationManager alloc]init];

        locationManager.delegate=self;
        [locationManager startUpdatingLocation];
    }
    if ([CLLocationManager regionMonitoringAvailable]) {//check service is available or not

        [self startregionMonitoring];
    }

    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark - GET Location
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation");
    NSDate *eventData=newLocation.timestamp;
    NSTimeInterval howRecent=[eventData timeIntervalSinceNow];

    if (abs((howRecent)<15.0)) {
        NSLog(@"latitude %+.6f, longitude %+.6f \n",newLocation.coordinate.latitude,newLocation.coordinate.longitude);

        CLLocationDistance dist=[newLocation distanceFromLocation:oldLocation]/1000;
        NSLog(@"===================distance %5.1f traveled",dist);
        latitude=newLocation.coordinate.latitude;
        NSLog(@"lat :%+.5f",latitude);
        longituted=newLocation.coordinate.longitude;
        NSLog(@"long %+.6f",longituted);
    }

}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [locationManager stopUpdatingLocation];

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [locationManager startUpdatingLocation];

}

now where you want latitude and longitude you can access [DELEGATE latitude];,[DELEGATE longitude]; where DELEGATE Is #define DELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate] 
于 2012-12-16T05:05:20.067 に答える