2

我在 RootViewController.m 上有一个选项卡。该选项卡有 2 个按钮。单击后的第一个按钮将转到带有 mapView 的 CorpViewcontroller。当我第一次尝试点击第一个按钮时,地图是空白的,底部有谷歌标签。我必须单击返回,然后再次单击按钮,然后地图就会显示出来。是否可以始终在第一次单击按钮时显示地图?

我的 rootViewController.m 进入第二个屏幕:

  [self.navigationController pushViewController:self.corpController animated:YES];

名为 corpViewController 的第二个屏幕具有以下代码:

  - (void)viewDidLoad
  {
    [super viewDidLoad];

    self.title = @"Set Remote Location";
    self.jsonData = [[NSMutableData alloc] init];

    mapView.showsUserLocation = YES;

    //Setup the double tap gesture for getting the remote location..
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
                               initWithTarget:self action:@selector(handleGesture:)];
    tgr.numberOfTapsRequired = 2;
    tgr.numberOfTouchesRequired = 1;
    [mapView addGestureRecognizer:tgr];

    mapView.delegate = self;

    NSLog(@"viewDidLoad  done");
    }


    - (void)viewWillAppear:(BOOL)animated    {

      NSLog(@"viewWillAppear");

      appDelegate = (NBSAppDelegate *)[[UIApplication sharedApplication] delegate];

      double curLat = [appDelegate.curLat doubleValue];

      MKUserLocation *userLocation = mapView.userLocation;

      double miles = 10.0;
      double scalingFactor = ABS( (cos(2 * M_PI * curLat / 360.0) ));

      MKCoordinateSpan span; 

      span.latitudeDelta = miles/69.0;
      span.longitudeDelta = miles/(scalingFactor * 69.0); 

      MKCoordinateRegion region2;
      region2.span = span;
      region2.center = userLocation.coordinate;

      [mapView setRegion:region2 animated:YES];

       NSLog(@"viewWillAppear done..");

       }

请指教。

谢谢

4

2 回答 2

3

viewDidLoad您是否在视图控制器的方法中初始化 MapView ?

如果是这样,请尝试将其移至该viewDidAppear方法。这对我有用。

于 2013-05-07T01:14:49.123 回答
2

viewDidLoad您设置showsUserLocationYES和 时viewWillAppear,您正在放大mapView.userLocation坐标。

在设置为后,该userLocation属性通常不会立即准备好有效坐标。showsUserLocationYES

第一次显示视图控制器时,它仍然无效,并且您正在放大坐标 0,0。

当您第二次显示视图控制器时,用户位置已经获得并且坐标是有效的。

与其放大用户位置,不如在地图视图获取用户位置更新时调用viewWillAppear的委托方法中进行。mapView:didUpdateUserLocation:

此外,您可能还想将mapView.showsUserLocation = YES;toviewWillAppear和 in移至viewWillDisappear,将其设置为NO。这样,每次显示视图控制器时,地图视图都会放大到用户位置,而不仅仅是第一次显示。


一个不相关的一点是,要放大到特定距离,使用该功能要容易得多,MKCoordinateRegionMakeWithDistance而不是自己尝试将英里转换为度数。


以下是建议的更改示例corpViewController

- (void)viewWillAppear:(BOOL)animated
{
    //move this from viewDidLoad to here...
    mapView.showsUserLocation = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    mapView.showsUserLocation = NO;
}

-(void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)userLocation
  //Changed the **internal** parameter name from mapView to mv
  //to avoid a compiler warning about it hiding instance var with same name.
  //It's better to use the passed parameter variable anyway.
{
    NSLog(@"didUpdateUserLocation");

    double miles = 10.0;

    //Instead of manually calculating span from miles to degrees, 
    //use MKCoordinateRegionMakeWithDistance function...
    //Just need to convert miles to meters.
    CLLocationDistance meters = miles * 1609.344;
    MKCoordinateRegion region2 = MKCoordinateRegionMakeWithDistance
                                   (userLocation.coordinate, meters, meters);

    [mv setRegion:region2 animated:YES];
}
于 2012-05-10T01:57:33.870 回答