2013-07-17 72 views
1

新的目標c!它爲什麼不放大?

我試圖讓我的mapview放大。我從書中的作業中複製了一些代碼,但它不會縮放。只顯示unzoomed mapview。

有什麼建議嗎?請參閱下面的viewController代碼。

另外 - 關於導入頭文件和@class指令差異的幾句話會很棒!?

在此先感謝

#import "TrackViewController.h" 
#import "MainWindowViewController.h" 
#import <MapKit/MapKit.h> 



@class MainWindowViewController; 
@implementation TrackViewController 

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{  
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

if(self) 
{ 
    locationManager = [[CLLocationManager alloc]init]; 
    [locationManager setDelegate:self]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
} 
    return self; 
} 

-(IBAction) back:(id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void) findLocation; 
{ 
    [locationManager startUpdatingLocation]; 
} 

-(void) foundLocation:(CLLocation *)loc 
{ 
    CLLocationCoordinate2D coord = [loc coordinate]; 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 100, 100); 
    [worldView setRegion:region]; 
    [locationManager stopUpdatingLocation]; 
} 

-(void)viewDidLoad 
{ 
    [worldView setShowsUserLocation:YES]; 
    [worldView setMapType:MKMapTypeHybrid];  
} 

-(void)dealloc 
{ 
    [locationManager setDelegate:nil]; 
} 

- (void) locationManager:(CLLocationManager *) manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
{ 

NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow]; 
if(t<180){ 
    return; 
} 
    [self foundLocation:newLocation]; 
} 

-(void)locationManager:(CLLocationManager *)manager 
    didFailWithError:(NSError *)error 
{ 
    NSLog(@"Could not find location: %@", error); 
} 

@end 

回答

2

我發現的唯一的事情就是你的setRegion行更改爲:

[worldView setRegion:region animated:YES];//instead of just setRegion 

-The setRegion:文檔狀態:

The area currently displayed by the map view

- 而setRegion:animated:文件entation指出:

Changes the currently visible region and optionally animates the change

否則,我建議打印出您的CLLocationCoordinate2D對象,並確保它是有效的。

+0

我剛剛使用setRegion? 我應該在哪裏嘗試打印CLLocationCoordinate2d對象? – Anders

+0

它是一種不同的方法。 setRegion與setRegion不同:動畫: –