Ios 仅在一个设备上出现无效区域错误

Ios 仅在一个设备上出现无效区域错误,ios,maps,mapkit,cllocationmanager,region,Ios,Maps,Mapkit,Cllocationmanager,Region,我尝试在我的应用程序中显示地图,它在我的模拟器和iPodtouch4G上工作 但我的iPhone总是出错,我不知道为什么会发生这种情况。 也许你能解释一下它为什么失败。 错误是: 由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:“无效区域” 我已经在google和stackoverflow上搜索了解决方案,但似乎没有人有这个问题 这是我的密码: //setup mapview mapView.delegate = self; mapView.shows

我尝试在我的应用程序中显示地图,它在我的模拟器和iPodtouch4G上工作

但我的iPhone总是出错,我不知道为什么会发生这种情况。 也许你能解释一下它为什么失败。 错误是:

由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:“无效区域

我已经在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%正确,但它确实有效。

您使用的是什么版本的iPhone?它有视网膜显示器吗?每英里的米数常数是多少?那你为什么要把它乘以0.5呢?这是一个带iOS 6的iPhone4S,所以它有一个视网膜显示器。每英里米数定义为:#定义每英里米数1609.344您在哪里呼叫startUpdatingLocation?你不应该尝试使用位置管理器。在创建它之后,我会考虑自己回答这个问题,接受它,这样它不会影响你的接受评级。听起来你已经明白了。