我试图在视图加载后将地图缩放到用户的当前位置,但出现错误“*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无效区域”“当视图加载时。有人可以帮忙吗?
Cheers!
- (void)viewDidLoad
{
[super viewDidLoad];
MKCoordinateRegion mapRegion;
mapRegion.center.latitude = map.userLocation.coordinate.latitude;
mapRegion.center.longitude = map.userLocation.coordinate.longitude;
mapRegion.span.latitudeDelta = 0.2;
mapRegion.span.longitudeDelta = 0.2;
[map setRegion:mapRegion animated: YES];
}
你设置了吗showsUserLocation = YES
? MKMapView
如果设置为,则不会更新位置NO
。所以请确保这一点。
很有可能的是MKMapView
对象还没有用户位置。为了做正确的事,你应该采用MKMapViewDelegate
协议和实施mapView:didUpdateUserLocation:
map.delegate = self;
...
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.2;
mapRegion.span.longitudeDelta = 0.2;
[mapView setRegion:mapRegion animated: YES];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)