例外:'无效区域<center:+ inf,+ 0.00000000 =“”span:+ 1.00000000,=“”+0.50000000 =“”>'尝试显示地图时</center:+ inf,>

时间:2011-05-13 14:16:45

标签: ios mapkit

当我尝试显示地图时,我得到了这个例外:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:+inf, +0.00000000 span:+1.00000000, +0.50000000>'

我的相关代码是:

-(void)viewWillAppear:(BOOL)animated
{   
    [mapView removeAnnotations:mapView.annotations];

    // locationManager update as location
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    //Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    latitudeOfUserLocation=coordinate.latitude;
    longitudeOfUserLocation=coordinate.longitude;


    location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
    MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
    annotation.pinColor = MKPinAnnotationColorRed;  
    [mapView addAnnotation:annotation];

    MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
    MKCoordinateRegion region={location2D,span};
    [mapView setRegion:region];

}

我无法弄清楚如何解决这个问题,提前:)

编辑:

嗨再次,我尝试按照你说的做,异常已经解决,但是,当我尝试在控制台中显示用户的经度/纬度我什么都没有时,这是我的代码非常正确:< / p>

    -(void)viewWillAppear:(BOOL)animated
    {  

        [mapView removeAnnotations:mapView.annotations];

        // locationManager update as location
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        [locationManager startUpdatingLocation];
        NSURL *url=[NSURL         URLWithString:@"http://ipho.franceteam.org/ad_V1/stations/"];
    ASIFormDataRequest * request=[ASIFormDataRequest requestWithURL:url];
[request setPostValue:[NSNumber numberWithFloat:longitudeOfUserLocation] forKey:@"longitude"];
    [request setDelegate:self];
    [request startAsynchronous];
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText=@"Recherche en cours..";// this never stop loading

    }

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *location = [locationManager location];
    //Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];
    latitudeOfUserLocation=coordinate.latitude;
    longitudeOfUserLocation=coordinate.longitude;
    NSLog(@"your latitude :%f",latitudeOfUserLocation);//here nothing is shown
    NSLog(@"your longitude :%f",longitudeOfUserLocation);// and here too
    location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
    MyLocation *annotation=[[[MyLocation alloc]initWithName:@"Vous êtes ici" distanceVersLaStation:@"" coordinate:location2D]autorelease];
    annotation.pinColor = MKPinAnnotationColorRed;  //or red or whatever
    [mapView addAnnotation:annotation];
    //
    MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
    MKCoordinateRegion region={location2D,span};
    [mapView setRegion:region];

}//End function

即使我在模拟器上工作,我应该在控制台上得到一些东西吗?

编辑: 嗨再次,我有机会在iPhone(设备)上测试我的代码,我注意到当我尝试搜索时,应用程序已经过程中跟踪我的位置并找到符合我搜索的站点(紫色注释),但是,地图不会显示,搜索进度仍在加载,永远不会停止。

hud.labelText=@"Recherche en cours..";// this never stop loading

这是一个可以更好地解释的截图:

enter image description here

5 个答案:

答案 0 :(得分:12)

在某些情况下(当应用从后台变为活动状态时)会触发didUpdateUserLocation方法,但不会更新位置。在这些情况下,没有有效的区域,并且setRegion:方法可以抛出异常。 愚蠢的,一个简单的解决方案是在设置之前检查其区域是否有效:

 if(region.center.longitude == -180.00000000){
    NSLog(@"Invalid region!");
}else{
    [aMapView setRegion:region animated:YES];
}

答案 1 :(得分:12)

“无效区域”异常是因为您设置为mapView的区域无效。据我所知,有两个原因可能导致此异常:区域的中心点无效,或区域的跨度无效。

避免这种例外:
1)检查中心点值:纬度在[-90; 90]范围内,经度在[-180; 180]范围内 2)使用[mapView regionThatFits:region]获取有效跨度的区域,然后设置为mapview:
[mapView setRegion:[mapView regionThatFits:region]]

答案 2 :(得分:4)

调用startUpdatingLocation后,可能需要几秒钟才能更新位置,因此您无法立即尝试检索该位置。在更新之前,位置包含无效值,这是错误告诉您的。

相反,实现locationManager:didUpdateToLocation:fromLocation:委托方法并阅读其中的位置。

startUpdatingLocation之后的所有代码移动到该方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *location = [locationManager location];
    //etc...
}

注意:以上方法已被删除: Apple Doc

答案 3 :(得分:1)

[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];

答案 4 :(得分:0)

不能大于180

CLLocationDegrees delta = MAX(maxLatitude - minLatitude, maxLongitude - minLongitude) * 1.1;

delta = points.count < 2 ? 0.5 : delta;
delta = MIN(delta, 180);

CLLocationDegrees centerX = (maxLatitude-minLatitude)/2.0 + minLatitude;
CLLocationDegrees centerY = (maxLongitude-minLongitude)/2.0 + minLongitude;

CLLocationCoordinate2D theCoordinate = {centerX,centerY};
MKCoordinateSpan span = MKCoordinateSpanMake(delta, delta);
self.mapView.region = MKCoordinateRegionMake(theCoordinate, span);