在应用程序的多个部分中呼叫位置



我想使用以下代码在我的应用的多个部分中查找用户的位置。为了避免复杂性,我只想有一个地方来获取用户的位置。我假设我需要把它放在我的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];
}
将所有

位置侦听器和相关内容移动到应用程序委托,然后将NSString *location属性设为 AppDelegate ,现在,只要您获得更新的位置,就会将其设置为 NSString *location ,在您的其他ViewControllers中,当您需要时,访问应用程序委托的位置属性。

如果您确实需要更新的位置,则可以使用 NSNotificationCenter 并广播有关新位置更新的通知,因此真正需要更新位置的ViewController将自行注册有关新位置的通知,并相应地执行操作。

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] 

最新更新