0

I have tried using this method, but for some reason it wont zoom in on the current location. Is there something I am doing wrong?

Appdelegate.m

 -(void)handleNetworkChange
{
    self.reachability = [Reachability reachabilityForInternetConnection];
    [self.reachability startNotifier];
    NetworkStatus remoteHostStatus = [self.reachability currentReachabilityStatus];
    MapViewController *mapViewController = [[MapViewController alloc]init];
    if(remoteHostStatus == NotReachable)
    {
        self.internetReachable = NO;
    }
    else
    {
        self.internetReachable = YES;
        [mapViewController userLocation];
    }    
}

MapViewController.m

-(void)userLocation
{
    MKCoordinateRegion mapRegion;
    mapRegion.center.latitude = self.mapView.userLocation.coordinate.latitude;
    mapRegion.center.longitude = self.mapView.userLocation.coordinate.longitude;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;
    [self.mapView setRegion:mapRegion animated:YES];
}
4

1 回答 1

0

我会简单地使用 NSNotificationCenter。您设置了一个等待发布通知的侦听器,一旦收到发布通知,它将触发一个选择器

这是怎么做的(实际上很简单)

这需要在您呈现地图的视图控制器中进行,最好在 viewDidLoad

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

他的缩放方法应该放在同一个视图控制器中——像这样:

-(void)runThisFunctionWhenReady
{
   // Zoom the map and other funky stuff
}

现在,您可以从应用程序中的任何位置(包括委托)通过发布如下通知来触发它:

 [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];
于 2014-03-10T16:35:53.483 回答