iOS MapKit 通过 didUpdateUserLocation 崩溃 - 困惑
我需要一些关于我正在使用 MapKit 制作的应用程序的帮助,
我正在与 didUpdateUserLocation: - 它不断随机崩溃应用程序作斗争。当我注释掉所有代码时,它可以完美运行,但这不是合适的情况。我一直在摆弄这一切,但仍然没有真正的成功。
这是我的代码
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
NSLog(@"update loc");
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250.0, 250.0);//MKCoordinateRegionMake(userLocation.coordinate, mapView.region.span);
if (first) {
region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250.0, 250.0);
first = false;
}
if (!CLLocationCoordinate2DIsValid(userLocation.coordinate)) {
//do nothing, invalid regions
NSLog(@"co-ord fail");
} else if (region.span.latitudeDelta <= 0.0 || region.span.longitudeDelta <= 0.0) {
NSLog(@"invalid reg");
} else {
[mapView setRegion:region animated:NO];
}
}
应用程序永远不会遇到“co-ord failed”或“invalid reg”,所以我真的不知道问题是什么,因为我自己设置了值。
当地图的笔尖关闭并且已释放视图时,通常会出现此问题。
我看过其他一些建议,但对他们的工作没有信心。理想情况下,我想使用 mapView.region.span 来保持缩放级别
非常感谢, 詹姆斯
I need some help with an app i'm making using MapKit
I'm struggling with the didUpdateUserLocation: - it keeps randomly crashing the app. When I comment out all of the code it works perfectly but this isn't a suitable situation. I've been fiddling around with it all but still no real success.
Here's my code
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
NSLog(@"update loc");
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250.0, 250.0);//MKCoordinateRegionMake(userLocation.coordinate, mapView.region.span);
if (first) {
region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250.0, 250.0);
first = false;
}
if (!CLLocationCoordinate2DIsValid(userLocation.coordinate)) {
//do nothing, invalid regions
NSLog(@"co-ord fail");
} else if (region.span.latitudeDelta <= 0.0 || region.span.longitudeDelta <= 0.0) {
NSLog(@"invalid reg");
} else {
[mapView setRegion:region animated:NO];
}
}
The app never hits "co-ord fail" or "invalid reg" so I don't really know what the problem is since I set the values myself.
The problem usually occurs when the nib for the Map is closed and it has dealloced the view.
I have seen some other suggestions but haven't been confident about their work. Ideally I'd like to use mapView.region.span so to maintain the zoom levels
Many Thanks,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,如果应用程序暂停了一小会儿,则在恢复时,
委托会收到第一个用户位置 (0.0,0.0) 作为纬度、经度!我认为这是一个错误.. 检查:
结果:
奇怪的是 0,0 是从 userLocation.location.coordinate 生成的,而 userLocation.location 为零,甚至产生有效的坐标!
事实上,检查 userLocation.location 为零似乎是最好的。
Watch out that if an App that was suspended a short while, on resume the
delegate receives as the first userLocation (0.0,0.0) as latitude,longitude! I think that is a bug.. Check:
Result:
Odd that 0,0 is produced from userLocation.location.coordinate while userLocation.location is nil, resulting in a valid coordinate, even!
Indeed the check on userLocation.location being nil seems best.
那这不是你的问题吗?
这就是为什么我们有观察者/监听者这样的依赖倒置模式。
不要直接使用 CLLocationManager 委托摆弄视图,而是使用 NSNotification 来提醒订阅视图(例如 MapView)它应该更新。如果 MapView 已被释放,它将不会执行任何操作。
Well isn't that your problem then?
This is why we have dependency inversion patterns like Observer/Listener.
Instead of fiddling around with the view directly with your CLLocationManager delegate, use NSNotification to alert subscribing views (such as your MapView) that it should update. If the MapView has been dealloced it won't do anything.