2012-04-04 51 views
0

我已經得到了具有引腳位置的地圖應用程序,但我想改變它,以便放大該用戶的位置。MapKit變焦問題

默認情況下,出發在座標0,0(位於非洲海岸),但現在它的家園在英國,但我不能得到它放大到街道級別。當我調整Delta設置時,它沒有任何區別。

這裏是我使用的代碼。

#import "FirstViewController.h" 
#import "MapPin.h" 

@implementation FirstViewController 

@synthesize map; 
@synthesize locationManager , location; 


-(void)addAnnotations{ 

    // Normally read the data for these from the file system or a Web service 
    CLLocationCoordinate2D coordinate = {53.0670, -2.521}; 
    MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate 
              placeName:@"Mr Shoe - Gents,Ladies,Kids" 
             description:@"01270 626767, 123 Your Street, CW5 5NA" 
        ]; 

    [self.map addAnnotation:pin]; 

    [pin release]; 

    // Normally read the data for these from the file system or a Web service 
    CLLocationCoordinate2D coordinate2 = {53.0659, -2.521}; 
    MapPin *pin2 = [[MapPin alloc]initWithCoordinates:coordinate2 
              placeName:@"Veg-is-us - Fruit & veg" 
              description:@"01270 626767, 123 Your Street, CW5 5NA" 

        ]; 

    [self.map addAnnotation:pin2]; 


    [pin2 release]; 

    CLLocationCoordinate2D coordinate3= {53.06879, -2.52195}; 
    MapPin *pin3= [[MapPin alloc]initWithCoordinates:coordinate3 
              placeName:@"PC Centre Nantwich" 
              description:@"01270 626767, 15c Beam street, CW5 5NA" 

        ]; 

    [self.map addAnnotation:pin3]; 


    [pin3 release]; 
} 

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
/* 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
// Custom initialization. 
} 
return self; 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation]; 
    [self addAnnotations]; 
     [self addAnnotations]; 


    [super viewDidLoad]; 
} 
-(void) locationManager: (CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    location = newLocation.coordinate; 
    //One location is obtained.. just zoom to that location 

    MKCoordinateRegion region; 
    region.center=location; 
    //Set Zoom level using Span 
    MKCoordinateSpan span; 
    span.latitudeDelta=0.4; 
    span.longitudeDelta=0.4; 
    region.span=span; 
    NSLog(@"map=%@", map); 
    [map setRegion:region animated:TRUE]; 

} 

/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations. 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 
+0

人?它一定是一個簡單的錯誤,但我看不到它, – user1313926 2012-04-05 09:33:19

+0

代碼看起來沒問題。你確定委託方法正在被調用(在那裏放置一個斷點或NSLog)?在那個委託方法中,就在setRegion行之前,把'NSLog(@「map =%@」,map);'看看它打印的是什麼。您是否正在更新應用中其他地方的地圖區域? – Anna 2012-04-05 14:06:38

+0

@MDT這是我的代碼,我試圖輸出到調試,但它似乎沒有記錄它,因爲我升級的Xcode(舊調試控制檯確定工作) – user1313926 2012-04-05 16:05:13

回答

0

viewDidLoad,這條線沒有做任何事情:

[[CLLocationManager alloc] init]; 

編譯器應該被給予警告,如在該行「表達式結果未使用」。


實際上你需要指定代碼到locationManager變量的結果,你需要設置delegate否則委託方法將不會被調用:

self.locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 


順便說一句,在viewDidLoad,您呼叫[self addAnnotations];兩次,我認爲這是更好地稱之爲[super viewDidLoad]第一,而不是最後一次。

+0

感謝@Anna我已經渡過它一點,它和它現在放大其中很棒,非常感謝您爲我節省更多頭部撞擊,但是我現在已經失去了在地圖上導航的能力,它將橡皮筋帶回中心。有任何想法嗎? – user1313926 2012-04-05 21:14:06

+0

每當用戶位置更新和其中的代碼不斷重新定位到用戶時,didUpdateToLocation方法將繼續得到調用。要放大第一次更新,請在該方法結尾處調用'[manager stopUpdatingLocation];'。 – Anna 2012-04-05 21:20:12

+0

你是一個救世主......非常感謝 – user1313926 2012-04-05 21:25:35