iphone app - how to get the current location only once and store that to be used in another function?

风格不统一 提交于 2019-12-03 06:20:22
Aitul

.h file

#import <CoreLocation/CoreLocation.h>

@property (nonatomic,retain) CLLocationManager *locationManager;

.m file

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

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

The easy way to do that is to store the starting current location in NSUserDefaults

     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:latitude forKey:@"starLatitude"];
    [defaults setObject:longitude forKey:@"starLongitude"];
[defaults synchronize];

Then you can get the latitude and longitude in the following way:

NSString *lati = [defaults objectForKey:@"starLatitude"];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!