MapView 未缩放至用户位置
我的地图视图未缩放至我的用户位置。请指出错误:
- (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);
}
My mapview is not zooming to my user location. Please can you point out the error:
- (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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正在创建地图视图,但其框架尚未设置,并且未将其添加为视图控制器视图的子视图。
将 alloc+init 行更改为(根据需要更改尺寸):
然后在 PerformSelector 行之前添加以下内容:
但请注意,最好使用 MKMapView 的
didUpdateUserLocation
委托方法来缩放到用户的位置假设用户位置在某个任意的固定间隔(例如 1.25 秒)后可用。例如,删除performSelector行并实现didUpdateUserLocation:
使用didUpdateUserLocation方法唯一可能的问题是每次用户位置更改时都会调用它,因此如果您只想在启动时缩放一次,则需要添加一个BOOL标志ivar 并相应地检查/设置它。
The mapView is being created but its frame is not set and it's not being added as a subview of the view controller's view.
Change the alloc+init line to (change the dimensions as needed):
Then before the performSelector line, add this:
Note however that it's better to use the MKMapView's
didUpdateUserLocation
delegate method to zoom to the user's location instead of assuming the user location will be available after some arbitrary, fixed interval like 1.25 seconds.For example, remove the performSelector line and implement didUpdateUserLocation:
Only possible problem using the didUpdateUserLocation method is that it will be called every time the user's location changes so if you only want to zoom once on startup, you'll need to add a BOOL flag ivar and check/set it accordingly.