xcode MKMapView 在 Tab-Bar 应用程序 (iOS) 中一次但不是第二次放大用户的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8347607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MKMapView Zooming In On User's Location Once But Not The Second Time in Tab-Bar App (iOS)
提问by MillerMedia
I have an MKMapView as part of a Navigation Controller in a Tab Bar based app.
我有一个 MKMapView 作为基于标签栏的应用程序中导航控制器的一部分。
I click a UIButton on the first View Controller and it pushes to the second View Controller which contains the MKMapView. When the Map View loads, it zooms in on the user's location using:
我单击第一个视图控制器上的 UIButton 并将其推送到包含 MKMapView 的第二个视图控制器。当地图视图加载时,它使用以下方法放大用户的位置:
- (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if ( !initialLocation )
{
self.initialLocation = userLocation.location;
MKCoordinateRegion region;
region.center = theMapView.userLocation.coordinate;
region.span = MKCoordinateSpanMake(2.0, 2.0);
region = [theMapView regionThatFits:region];
[theMapView setRegion:region animated:YES];
}
}
When I hit the back button on the Navigation Controller above the MapView and then click back to the map, it no longer zooms in on the user's current location, but just has the full zoom out default:
当我点击 MapView 上方导航控制器上的后退按钮,然后单击返回地图时,它不再放大用户的当前位置,而是具有完全缩小默认值:
Here's a picture of the view the second time.
I figure it would work correctly if I could somehow call the didUpdateUserLocation in the viewDidAppear method but I'm not sure how to pull this off since the didUpdateUserLocation is a delegate method.
我认为如果我能以某种方式在 viewDidAppear 方法中调用 didUpdateUserLocation ,它会正常工作,但我不知道如何实现这一点,因为 didUpdateUserLocation 是一个委托方法。
Is that the right approach or is there a different approach I should take to do this? Thanks!
这是正确的方法还是我应该采取不同的方法来做到这一点?谢谢!
P.S. I've seen this question but it's slightly different with it's use of a modal view controller
回答by Mark Adams
I would pull all of the zooming code into its own method that can be messaged from -viewDidAppear:
and -mapView:didUpdateToUserLocation:
.
我会将所有缩放代码拉入它自己的方法中,该方法可以从-viewDidAppear:
和 发送消息-mapView:didUpdateToUserLocation:
。
- (void)zoomToUserLocation:(MKUserLocation *)userLocation
{
if (!userLocation)
return;
MKCoordinateRegion region;
region.center = userLocation.location.coordinate;
region.span = MKCoordinateSpanMake(2.0, 2.0); //Zoom distance
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
Then in -viewDidAppear:
...
然后在-viewDidAppear:
...
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self zoomToUserLocation:self.mapView.userLocation];
}
And in the -mapView:didUpdateToUserLocation:
delegate method...
在-mapView:didUpdateToUserLocation:
委托方法中......
- (void)mapView:(MKMapView *)theMapView didUpdateToUserLocation:(MKUserLocation *)location
{
[self zoomToUserLocation:location];
}