2013-07-18 43 views
0

我試圖用一組給定的座標打開地圖。它不工作。它僅在我退出視圖並再次輸入時纔有效。Apple Mapkit:地圖不以我給定的座標開頭

這裏是我的代碼 -

mapViewController.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface mapViewController : UIViewController <MKMapViewDelegate> 
@property (strong, nonatomic) IBOutlet MKMapView *mapView; 

@end 

mapViewController.m

#import "mapViewController.h" 

@interface mapViewController() 

@end 

@implementation mapViewController 
@synthesize mapView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.mapView.delegate = self; 

    CLLocationCoordinate2D zoomLocation; 
    zoomLocation.latitude = 39.281516; 
    zoomLocation.longitude= -76.580806; 

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(zoomLocation, 800, 800); 
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; 
} 


@end 
+0

如果在'viewDidLoad'的最後一行設置'animated:NO',會發生什麼?如果這不起作用,您可以嘗試移動代碼以將地圖縮放到「viewWillAppear」或「viewDidAppear」。 –

+0

:)我將代碼添加到viewDidAppear和BANG!它的工作,謝謝! –

+0

@JakeSpencer你可能想添加這個答案。它爲什麼與viewDidAppear一起工作? –

回答

1

有時我發現,動畫屏幕方式運行時,不能按預期工作與視圖控制器轉換同時進行。嘗試移動代碼以將地圖縮放到viewDidAppear