我正在尝试在我的应用程序中显示地图,它适用于我的模拟器和我的 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。
希望你能帮我
解决方案:
我太早使用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% 正确,但它确实有效。