iOS MapKit 通过 didUpdateUserLocation 崩溃 - 困惑

发布于 2024-12-25 21:23:40 字数 1142 浏览 3 评论 0原文

我需要一些关于我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2025-01-01 21:23:40

请注意,如果应用程序暂停了一小会儿,则在恢复时,

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 

委托会收到第一个用户位置 (0.0,0.0) 作为纬度、经度!我认为这是一个错误.. 检查:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
NSLog( @"userLocation:(%p) %@",userLocation,userLocation);
NSLog( @"userLocation.location:(%p) %@",userLocation.location,userLocation.location);
NSLog( @"userLocation.coordinate: %f,%f valid:%d",userLocation.coordinate.latitude,userLocation.coordinate.longitude,  CLLocationCoordinate2DIsValid(userLocation.coordinate));
NSLog( @"userLocation.location.coordinate: %f,%f valid: %d",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude, CLLocationCoordinate2DIsValid(userLocation.location.coordinate));

结果:

userLocation:(0x193fc0) <MKUserLocation: 0x193fc0>
userLocation.location:(0x0) (null)
userLocation.coordinate: -180.000000,-180.000000 valid:0
userLocation.location.coordinate: 0.000000,0.000000 valid: 1

奇怪的是 0,0 是从 userLocation.location.coordinate 生成的,而 userLocation.location 为零,甚至产生有效的坐标!

事实上,检查 userLocation.location 为零似乎是最好的。

Watch out that if an App that was suspended a short while, on resume the

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 

delegate receives as the first userLocation (0.0,0.0) as latitude,longitude! I think that is a bug.. Check:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
NSLog( @"userLocation:(%p) %@",userLocation,userLocation);
NSLog( @"userLocation.location:(%p) %@",userLocation.location,userLocation.location);
NSLog( @"userLocation.coordinate: %f,%f valid:%d",userLocation.coordinate.latitude,userLocation.coordinate.longitude,  CLLocationCoordinate2DIsValid(userLocation.coordinate));
NSLog( @"userLocation.location.coordinate: %f,%f valid: %d",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude, CLLocationCoordinate2DIsValid(userLocation.location.coordinate));

Result:

userLocation:(0x193fc0) <MKUserLocation: 0x193fc0>
userLocation.location:(0x0) (null)
userLocation.coordinate: -180.000000,-180.000000 valid:0
userLocation.location.coordinate: 0.000000,0.000000 valid: 1

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.

牵你手 2025-01-01 21:23:40

该问题通常发生在地图的笔尖关闭并且它
已解除分配视图。

   [mapView setRegion:region animated:NO];

那这不是你的问题吗?

这就是为什么我们有观察者/监听者这样的依赖倒置模式。

不要直接使用 CLLocationManager 委托摆弄视图,而是使用 NSNotification 来提醒订阅视图(例如 MapView)它应该更新。如果 MapView 已被释放,它将不会执行任何操作。

The problem usually occurs when the nib for the Map is closed and it
has dealloced the view.

   [mapView setRegion:region animated:NO];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文