这是我如何处理它。在获取位置后,我将它存储在NSDictionary中。然后,如果我需要再次定位,则返回NSDictionary而不是重新打开GPS。 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;
}
这不完全是我想要的。需要更多关于电池消耗的见解。 – 2012-04-06 20:32:03