- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我在使用 reverseGeocodeLocation 时遇到了一些奇怪的问题。这就是我所做的。 我从一个 view controller 推送到另一个。 View 出现后,我要求当前位置。在 did
关于抓取位置,iOS 模拟器经常不工作是否正常?每当我运行模拟器时,这个应用程序的项目都会在 50% 的时间内崩溃……但我似乎无法在代码本身中查明问题。如果问题确实出在代码本身,谁能帮我找到问题所在?
我的应用程序是基于地理位置的应用程序,tableview 中的每个条目都有条目的位置。我发现奇怪的是,当用户来自美国以外时,该位置是用他们的语言写的。例如,有一位来自越南的用户,地址是用越南语写的。我
这是我正在使用的代码。我不确定应该将哪些变量传递到完成处理程序中。我查看的所有其他帖子都说要传递“地标”和“错误”,但我遇到了警告和错误。 func locationManager(manager:
如果网络不可用,我在尝试对位置进行反向地理编码时看到奇怪的结果: 在 iOS 5.0.1 上看到 开启飞行模式 [geocoder reverseGeocodeLocation:whatever co
我有一个坐标数组,我使用 for 循环遍历这些坐标。我想在 map 上为每个位置放置注释,并将标注的副标题作为坐标的地址,通过使用 reverseGeocodeLocation 在 for 循环中,我
我从 Xcode 的方案中启用位置模拟检查 Allow Location Simulation 并从下拉列表中选择示例位置 - New York, NY, USA。 这是我从位置获取地点信息的代码。c
我在 iPhone 上使用 CLLocationManager 获取当前用户位置,然后使用 reverseGeocodeLocation 将位置转换为地址。 这工作正常,但结果以设备语言返回,我需要它
编辑解决方案:因为我怀疑 reverseGeocodeLocation 是异步的所以我不得不使用 dispatch_async( dispatch_get_main_queue
对于两个不同的坐标系,我无法从 CLGeocoder reverseGeocodeLocation 获得相同的结果。例如。 CLLocation *tempLocation = [[CLLocatio
我的 iPhone 应用程序应该根据用户的纬度和经度解析地址。reverseGeocodeLocation 工作正常,但结果是英文的。 有没有办法将结果本地化为其他语言? 无法在 apple 或其他任
这是我的代码和我尝试调用 reverseGeocodeLocation 的完整上下文: CLLocationCoordinate2D touchMapCoordinate = [self.mapVie
我正在使用 Corelocation 并更新基本的 long、lat 等以每秒查看尽可能多的 View ,这就是我想要的,但我现在引入了 reverseGeocodeLocation,我只希望它运行一
我想检测用户何时前往新国家/地区,因此打算将 startMonitoringSignificantLocationChanges: 与 reverseGeocodeLocation: 结合使用。 但是
我是 Swift 开发的新手。我刚刚将现有的工作代码转换为 swift2,同时从 6 更新 Xcode 7。 private func decodeLocation(array: [AnyOb
我模拟了一个外国位置,并使用了CLGeocoder reverseGeocodeLocation(_ location:, completionHandler:),但是我得到了一个错误- [Clien
我想编写一个函数来对位置进行反向地理编码并将结果字符串分配给一个变量。正在关注this发布我有这样的东西: extension CLLocation { func reverseGeocode
问题是completionHandler block 中的代码永远不会运行;我使用了断点,程序会在构建模式下跳过完成处理程序 block 下面是 PinALandPlaceMark 中使用的两个函数,
我正在开发具有反向地理编码功能的 iOS 应用程序。当我第一次调用该函数时,一切都很好。第二次调用后(使用完成调用的 Controller 的新实例)出现“Domain=kCLErrorDomain
我想做的是将 CLLocation 传递给函数 getPlacemarkFromLocation,然后函数通过 reverseGeocodeLocation 使用传递的 CLLocation 设置将返
我是一名优秀的程序员,十分优秀!