第二次尝试使用 didUpdateUserLocation 时,mapView 未缩放

发布于 2024-11-13 07:02:50 字数 1034 浏览 1 评论 0原文

当从我的主视图控制器按下视图地图按钮时,我正在加载地图视图控制器:

- (IBAction)mapButton:(id)sender {
    MapKitDisplayViewController *mapKitDisplayView = [[MapKitDisplayViewController alloc] init];
    [self presentModalViewController:mapKitDisplayView animated:YES];   
    [mapKitDisplayView release]; mapKitDisplayView = nil;
}

在此地图视图启动时,调用此方法,它会正确缩放到用户位置:

- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    myMapView.showsUserLocation = YES;
    NSLog(@"didUpdateUserLocation = '%@'", userLocation);

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 20000, 20000);
    [myMapView setRegion:region animated:YES];
}

我在此屏幕上有一个完成按钮,可以关闭此视图:

- (IBAction)mapDoneButton:(id)sender 
{    
    [self dismissModalViewControllerAnimated:YES];
}

在此阶段用户被带回主视图控制器,但是当我再次按下视图地图按钮时,永远不会调用 didUpdateUserLocation 方法!因此,地图视图会缩小到默认视图,并且不会像以前一样放大。

我怎样才能让它再次被调用?

谢谢

i am loading a mapviewcontroller when the view map button is pressed from my mainviewcontroller:

- (IBAction)mapButton:(id)sender {
    MapKitDisplayViewController *mapKitDisplayView = [[MapKitDisplayViewController alloc] init];
    [self presentModalViewController:mapKitDisplayView animated:YES];   
    [mapKitDisplayView release]; mapKitDisplayView = nil;
}

On startup of this map view this method is called, which correctly zooms to the users location:

- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    myMapView.showsUserLocation = YES;
    NSLog(@"didUpdateUserLocation = '%@'", userLocation);

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 20000, 20000);
    [myMapView setRegion:region animated:YES];
}

I have a done button on this screen that dismisses this view:

- (IBAction)mapDoneButton:(id)sender 
{    
    [self dismissModalViewControllerAnimated:YES];
}

At this stage the user is taken back to the main view controller, however when i press the view map button again, the didUpdateUserLocation method is never called! Hence, the map view is zoomed out to the default one and wont zoom in as before.

How can i get it to be called again?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

薄荷港 2024-11-20 07:02:50

MKMapView 对象本身不会触发用户位置更新。您需要通过设置 showsUserLocation = YES 来激活搜索。然后,这将获取用户的位置,然后调用委托方法 mapView:didUpdateUserLocation: 。因此,在 viewWillAppear: 上将此值设置为 YES,一旦获得用户位置,就可以将其设置为 NO 以停止跟踪用户。如果您不这样做,它会定期更新用户位置并调用委托方法。如果不这样做,用户位置应该是不可靠的。

MKMapView object doesn't trigger an update on user location on its own. You need to activate the search by setting showsUserLocation = YES. This will then fetch the user's location and then the delegate method mapView:didUpdateUserLocation: is called. So set this value to YES on viewWillAppear: and once you've the user location you can set it to NO to stop tracking the user. If you don't do that, it periodically updates the user location and calls the delegate method. Without doing this, user location should be unreliable.

踏月而来 2024-11-20 07:02:50

接下来我要做的是:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotUserLocation" object:self];}

然后在 viewDidLoad 中检查用户位置是否可用。如果没有将视图控制器注册为该通知的接收者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zoomToLocation) name:@"gotUserLocation" object:nil];

如果用户位置可用,只需调用 ZoomToLocation 方法

What i do is next:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotUserLocation" object:self];}

then in viewDidLoad check of user location is available. if it is not register view controller as reciever for that notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zoomToLocation) name:@"gotUserLocation" object:nil];

if user location is available just call zoomToLocation method

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