これは私の扱いです。私は場所を取得した後、私はそれを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;
}
これは私が欲しかったものではありません。バッテリーの消耗についてより多くの洞察が必要です。 –