mapView无法缩放,使用didUpdateUserLocation进行第二次尝试 [英] mapView not zooming on second try using didUpdateUserLocation

查看:93
本文介绍了mapView无法缩放,使用didUpdateUserLocation进行第二次尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

谢谢

推荐答案

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.

这篇关于mapView无法缩放,使用didUpdateUserLocation进行第二次尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆