gpt4 book ai didi

ios - CoreLocations reverseGeocodeLocation 让它每分钟运行/循环一次

转载 作者:行者123 更新时间:2023-11-29 12:31:35 27 4
gpt4 key购买 nike

我正在使用 Corelocation 并更新基本的 long、lat 等以每秒查看尽可能多的 View ,这就是我想要的,但我现在引入了 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 没有错误,但我在屏幕上的 View 标签实际上从未使用来自地理服务器的数据进行更新,而且当 [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 代替上面方法中“//my timer”代码的位置时,得到了

"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);
}
}];

});

任何有助于弄清楚如何正确执行此操作的帮助都是很好的

最佳答案

您的第一个代码几乎是正确的,但您需要考虑到反向地理编码将在后台线程上完成并且您应该只更新主队列上的 UI。

if(self.reverseGeoLastUpdate == nil || [self.reverseGeoLastUpdate timeIntervalSinceNow] > 59 ) {
self.reverseGeoLastUpdate = [NSDate date];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
dispatch_async(dispatch_get_main_queue(), ^{
self.address1.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@\n",
placemark.subThoroughfare, placemark.thoroughfare,
//other 4 place marker calls here
];
});
} else {
NSLog(@"%@", error.debugDescription);
}

}];
}
}

此外,尝试改掉使用 _ 而不是 self 来访问属性的习惯,除非你故意想绕过 setter/getter

关于ios - CoreLocations reverseGeocodeLocation 让它每分钟运行/循环一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27523775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com