2012-07-30 49 views
1

這是我的代碼現在。我從位置管理器獲取回調,但它不想縮放到位置。使用跨度縮放到用戶位置

#import "MapViewController.h" 


@interface MapViewController() 

@property (nonatomic, strong) CLLocationManager *locationManager; 


@end 

@implementation MapViewController 

@synthesize mapView = _mapView; 
@synthesize mPlacemark = _mPlacemark; 
@synthesize location = _location; 
@synthesize mStoreLocationButton = _mStoreLocationButton; 
@synthesize locationManager = _locationManager; 

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

- (void)viewDidLoad { 

[super viewDidLoad]; 
mapView=[[MKMapView alloc] initWithFrame:self.view.frame]; 
//mapView.showsUserLocation=TRUE; 
mapView.delegate=self; 
[self.view insertSubview:mapView atIndex:0]; 

NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO"); 
if ([self locationManager] == nil) { 
    CLLocationManager *newLocationManager = [[CLLocationManager alloc] init]; 
    [newLocationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
    [newLocationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [self setLocationManager:newLocationManager]; 
} 

[[self locationManager] setDelegate:self]; 
[[self locationManager] startUpdatingLocation]; 
NSLog(@"Started updating Location"); 

} 


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

NSLog(@"Did update to location"); 
mStoreLocationButton.hidden=FALSE; 
location=newLocation.coordinate; 

MKCoordinateRegion region; 
region.center=location; 
MKCoordinateSpan span; 
span.latitudeDelta=0.01; 
span.longitudeDelta=0.01; 
region.span=span; 


[mapView setRegion:region animated:TRUE]; 


} 

我可以張貼的頭文件如果需要的話。主要是我只關心縮放。我只是不明白爲什麼它不起作用。

回答

1

您的代碼爲- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation顯示正確。

嘗試改變線

[self.view insertSubview:mapView atIndex:0]; 

到:

[self.view addSubview:mapView]; 

這可能是該MKMapView沒有重繪本身,因爲它是如何顯示在您的視圖。

+0

完美工作。謝謝。 – Coltrane 2012-07-30 23:23:42

+0

現在我注意到了另一個問題。現在改變這條線隱藏了我在地圖上的一個uibutton。我需要在mapview的頂部添加另一個子視圖嗎? – Coltrane 2012-07-30 23:31:51

+0

對不起,弄明白了。再次感謝您的回覆。 – Coltrane 2012-07-30 23:52:36