问题描述
我尝试在视图加载后将地图缩放到用户的当前位置,但我收到错误** *由于未捕获异常而终止应用NSInvalidArgumentException,原因:无效的地区'。
I'm trying to zoom a map into the user's current location once the view loads, but I'm getting the error "** * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region '" when the view loads. Please can someone help?
干杯!
- (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];
}
推荐答案
$ c> showsUserLocation = YES ? 如果设置为
不会更新位置。因此,请确保这一点。 NO
,则MKMapView
did you set showsUserLocation = YES
? MKMapView
won't update the location if it is set to NO
. So make sure of that.
这很可能是 MKMapView
对象没有用户位置。要做到正确,你应该采用 MKMapViewDelegate
协议并实现 mapView:didUpdateUserLocation:
It is very likely that the MKMapView
object doesn't have the user location yet. To do right, you should adopt MKMapViewDelegate
protocol and implement 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];
}
这篇关于MKMapView缩放到viewDidLoad上的用户位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!