【发布时间】:2014-12-17 10:46:56
【问题描述】:
Hiall我正在使用 Corelocation 并将基本的经纬度等更新为每秒尽可能多的视图,这就是我想要的,但我现在引入了 reverseGeocodeLocation,我只希望它运行/地理编码一次位置每分钟。目前它也在尽可能多地更新,Apple 认为这是不好的,并且只希望它每分钟更新一次。
有人可以告诉我我是怎么做的吗? (尽可能多地保持我的主要核心定位更新,但让我的 reverseGeocodeLocation 每分钟运行一次。)
我尝试了多种不同的方法来限制reverseGeocodeLocation runlike 与计时器的数量,如果语句和dispatch_after 没有成功,我尝试过的这些其他方法如下,我不确定是否需要执行暂停代码之类的操作不知何故,它做地理编码或???等等。我的核心定位代码是非常标准的 Apple 示例代码。 任何帮助都会很棒!
//下面的代码不显示所有代码,但相关部分等
.h
@interface ViewController : UIViewController <CLLocationManagerDelegate>
//I set other standard property outlets for location, locationManager, labels etc
@property (nonatomic, strong) NSDate *reverseGeoLastUpdate; //my 60second reversegeoupdates
.m
@implementation ViewController {
NSTimer *timer;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self startStandardUpdates];
}
- (void)startStandardUpdates {
if (nil == _locationManager)
_locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
NSLog(@"eventDate timeIntervalSinceNow = %f", [eventDate timeIntervalSinceNow]);
if (abs(howRecent) < 15.0) {
self.coordinateLat.text = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
//我的reverseGeocodeLocation计时器代码,尝试在开始时运行一次,然后每60秒运行一次 //这在开始时触发一次,因为 self.reverseGeoLastUpdate == nil 没有错误,但我在屏幕上的视图标签实际上从未使用来自地理服务器的数据进行更新,并且当 [self.reverseGeoLastUpdate timeIntervalSinceNow] > 60 时循环不会重复???????
if( [self.reverseGeoLastUpdate timeIntervalSinceNow] > 60 || self.reverseGeoLastUpdate == nil ){
NSLog(@"Resolving the Address: Loop run %d", _amountofruns++);
[geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
_address1.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@\n",
placemark.subThoroughfare, placemark.thoroughfare,
//other 4 place marker calls here
];
} else {
NSLog(@"%@", error.debugDescription);
}
}];
self.reverseGeoLastUpdate = [NSDate date];
}
}
当我尝试使用 NSTimer 代替上述方法中的“//我的计时器”代码但得到
"Error Domain=kCLErrorDomain Code=8 "操作无法完成"。
NSTimer scheduledTimerWithTimeInterval: 60.0
target: self
selector:@selector(onTick:)
userInfo: nil
repeats:YES];
-(void)onTick:(NSTimer *)timer {
[geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
_address1.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@\n",
placemark.subThoroughfare, placemark.thoroughfare,
//other 4 place marker calls here
];
} else {
NSLog(@"%@", error.debugDescription);
}
}];
当我尝试 dispatch_after 方式它触发并且标签得到更新,但是一旦触发它就会连续触发每一帧,并且无法弄清楚如何让它每 60 秒运行一次。
double delayInSeconds = 60.0;
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(delayTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^(void){
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
_address1.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@\n",
placemark.subThoroughfare, placemark.thoroughfare,
//other 4 placemarks
];
} else {
NSLog(@"%@", error.debugDescription);
}
}];
});
任何有助于弄清楚如何正确执行此操作的帮助都会很棒
【问题讨论】:
标签: ios objective-c loops core-location