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



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

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

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

方法如下(其实很简单)

这需要进入你的视图控制器你在那里呈现地图,最好在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];

最新更新