第二次尝试使用 didUpdateUserLocation 时,mapView 未缩放
当从我的主视图控制器按下视图地图按钮时,我正在加载地图视图控制器:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 settingshowsUserLocation = YES
. This will then fetch the user's location and then the delegate methodmapView:didUpdateUserLocation:
is called. So set this value toYES
onviewWillAppear:
and once you've the user location you can set it toNO
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.接下来我要做的是:
然后在 viewDidLoad 中检查用户位置是否可用。如果没有将视图控制器注册为该通知的接收者:
如果用户位置可用,只需调用 ZoomToLocation 方法
What i do is next:
then in viewDidLoad check of user location is available. if it is not register view controller as reciever for that notification:
if user location is available just call zoomToLocation method