내가 처리하는 방법입니다. 위치를 잡은 후에 NSDictionary에 저장합니다. 그런 다음 다시 위치를 지정해야하는 경우 GPS를 다시 켜는 대신 NSDictionary를 반환합니다. 2 분 후에 NSDictionary를 리셋합니다 (가장 좋은 스위트 룸이있는 시간을 조정할 수 있습니다). NSDictionary가 리셋 된 후 다음에 위치가 필요하면 GPS에서 새 위치를 가져옵니다.
- (NSDictionary *) getCurrentLocation {
if (self.currentLocationDict == nil) {
self.currentLocationDict = [[NSMutableDictionary alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *myLocation = [locationManager location];
[self.currentLocationDict setObject:[NSString stringWithFormat:@"%f", myLocation.coordinate.latitude] forKey:@"lat"];
[self.currentLocationDict setObject:[NSString stringWithFormat:@"%f", myLocation.coordinate.longitude] forKey:@"lng"];
[locationManager stopUpdatingLocation];
[locationManager release];
//Below timer is to help save battery by only getting new location only after 2 min has passed from the last time a new position was taken. Last location is saved in currentLocationDict
[NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(resetCurrentLocation) userInfo:nil repeats:NO];
}
return self.currentLocationDict;
}
- (void) resetCurrentLocation {
NSLog(@"reset");
[currentLocationDict release];
self.currentLocationDict = nil;
}
이것은 내가 원하는 것만은 아닙니다. 배터리 소모에 대해 더 많은 통찰력이 필요합니다. –