[英]MapView not zooming to user location
我的地圖視圖沒有縮放到我的用戶位置。 請您指出錯誤:
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] init];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
[self performSelector:@selector(startupZoom) withObject:nil afterDelay:1.25];
}
- (void)startupZoom {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=mapView.userLocation.coordinate.latitude;
location.longitude=mapView.userLocation.coordinate.longitude;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
NSLog(@"%f, %f", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude);
}
正在創建mapView,但未設置其框架,也沒有將其添加為視圖控制器視圖的子視圖。
將alloc + init行更改為(根據需要更改尺寸):
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
然后在performSelector行之前,添加以下內容:
[self.view addSubview:mapView];
但是請注意,最好使用MKMapView的didUpdateUserLocation
委托方法來縮放到用戶的位置,而不是假設用戶的位置在經過1.25秒的任意固定間隔后將可用。
例如,刪除performSelector行並實現didUpdateUserLocation:
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self startupZoom];
}
使用didUpdateUserLocation方法的唯一可能問題是,每次用戶位置更改時都會調用該方法,因此,如果您只想在啟動時縮放一次,則需要添加BOOL標志ivar並進行相應的檢查/設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.