【问题标题】:How to call a mapview current location zoom in method from appdelegate correctly?如何正确地从 appdelegate 调用 mapview 当前位置放大方法?
【发布时间】:2014-03-10 15:45:09
【问题描述】:

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

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

【问题讨论】:

  • 是您的方法 userLocation 从应用委托调用
  • 是的,我可以确认它正在被调用。
  • 在哪里显示(呈现)mapViewController,以及为什么每次调用handleNetworkChange 方法时都创建MapViewController 的实例?
  • 我仅以这种方式显示实例作为示例,但我显示 mapviewcontroller 的方式是通过导航控制器而不是通过 appdelegate。
  • 首先你可以记录 mapView userLocation 坐标,看看是否有正确的坐标,然后尝试将 deltas 从 0.2 更改为 0.005。

标签: ios objective-c mkmapview mkuserlocation


【解决方案1】:

我会简单地使用 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多