0

我想使用以下代码在我的应用程序的多个部分中查找用户的位置。为了避免复杂性,我只想在一个地方获取用户的位置。我会假设我需要将它放在我的 appDelegate 中,然后在需要使用它时从其他视图中以某种方式调用它。有谁知道我需要使用什么来将位置作为 NSString 在我的 appDelegate 以外的视图/选项卡上获取?谢谢!

- (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 回答