2

RootViewController.mにタブがあります。タブには2つのボタンがあります。クリックすると最初のボタンが、mapViewが表示されているCorpViewcontrollerに移動します。最初の試行で最初のボタンをクリックすると、マップは空白になり、下部にgoogleラベルが表示されます。戻るをクリックしてからもう一度ボタンをクリックすると、地図が表示されます。最初のボタンクリックで常に地図を表示することは可能ですか?

私のrootViewController.mは2番目の画面に移動します:

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

corpViewControllerと呼ばれる2番目の画面には、次のコードがあります。

  - (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 にズームインしています。

ビュー コントローラーを 2 回目に表示するまでに、ユーザーの位置が取得され、座標が有効になります。

でユーザーの位置をズームインする代わりに、マップ ビューがユーザーの位置の更新を取得したときに呼び出すviewWillAppearデリゲート メソッドで実行します。mapView:didUpdateUserLocation:

さらに、 を に移動mapView.showsUserLocation = YES;viewWillAppearviewWillDisappearに設定することもでき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 に答える