我正在尝试在我的应用程序中显示地图,它可以在我的模拟器和我的ipod touch 4g上运行。
但是我的iPhone总是出错,我不知道为什么会这样。 也许你可以解释一下它失败的原因。 错误是:
由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'无效区域
<center:nan, nan span:nan, nan>
'
我已经在google和stackoverflow上搜索了解决方案,但似乎没有人遇到这个问题。
这是我的代码:
//setup mapview
mapView.delegate = self;
mapView.showsUserLocation = YES;
//set up core location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//Set up zoom location
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = locationManager.location.coordinate.latitude;
zoomLocation.longitude= locationManager.location.coordinate.longitude;
debug(@"Location: %f und %f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude);
//zoom to location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[mapView setRegion:adjustedRegion animated:YES];
正如我所说,它适用于iPod和模拟器,但不适用于iPhone。
希望你能帮助我
SOLUTION:
我太早使用了locationManager,因此应用程序崩溃了。现在我用这种方式:
- (void)viewDidLoad {
[super viewDidLoad];
//setup mapview
mapView.delegate = self;
mapView.showsUserLocation = YES;
//set up core location
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//Set up zoom location
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = newLocation.coordinate.latitude;
zoomLocation.longitude= newLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[mapView setRegion:adjustedRegion animated:YES];
debug(@"Location: %f und %f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
}
不知道这种方式是否100%正确,但它确实有效。