如何调用mapview当前位置从appdelegate正确放大方法?

时间:2014-03-10 15:45:09

标签: ios objective-c mkmapview mkuserlocation

我尝试过使用此方法,但出于某种原因,它不会放大当前位置。有什么我做错了吗?

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

1 个答案:

答案 0 :(得分:0)

我只想使用NSNotificationCenter。 你设置了一个等待帖子通知的监听器,一旦收到帖子通知,它将触发一个选择器

这是如何做到的(实际上非​​常简单)

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

[[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(runThisFunctionWhenReady:) 
        name:@"TestNotification"
        object:nil];
他应该将zoom方法放在同一个viewcontroller中 - 就像这样:

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

现在,从应用程序中的任何位置(包括代理),您可以通过发布通知来解决此问题 像这样:

 [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];