Luca
Luca

Reputation: 20919

Exception : 'Invalid Region <center:+inf, +0.00000000 span:+1.00000000, +0.50000000>' when trying to display the map

When I try to display the map I get this exception :

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

My relevant code is this :

-(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];

}

Edit

I tried to do as you said in the comments. The exception is solved, however, when I try to display the longitude/latitude of the user in the console I get nothing. This is my code which is mostly correct:

    -(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

Even I work on the simulator, I should get something on the console right?

Edit

Hi again, i had the opportunity to test my code on iPhone (device) and i have noticed that when i try to search, the application has succeeded to track my position and to find me the stations that meet my search (purple annotation), however, the map isn't displayed and the searching progress is still loading and never stop.

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

Here is a screenshot that could explain better:

screenshot

Upvotes: 15

Views: 17906

Answers (5)

user467105
user467105

Reputation:

After calling startUpdatingLocation, it may take a few seconds for the location to be updated so you can't try to retrieve it immediately afterwards. Until it is updated, location contains invalid values which is what the error tells you.

Instead, implement the locationManager:didUpdateToLocation:fromLocation: delegate method and read the location in there.

Move all the code after startUpdatingLocation to that method:

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

Note: above method is depriciated: Apple Doc

Upvotes: 4

vencentle
vencentle

Reputation: 1

Can not be greater than 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);

Upvotes: 0

dormitkon
dormitkon

Reputation: 2526

In some situations (when the app becomes active from background) didUpdateUserLocation method is fired, but without updated location. In these cases there is no valid region, and setRegion: method can throw an exception. Stupid, a simple solution can be checking if its region is valid before you set it:

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

Upvotes: 13

Cao Huu Loc
Cao Huu Loc

Reputation: 1589

"Invalid Region" exception is through because the region you set to mapView is invalid. As I know now, there are 2 reasons can cause this exception: region's center point is invalid, or region's span is invalid.

To avoid this exception:
1) Check the center point value: latitude is in range [-90;90], longitude is in range [-180;180]
2) Use [mapView regionThatFits:region] to get a region with valid span then set to mapview:
[mapView setRegion:[mapView regionThatFits:region]]

Upvotes: 13

dave
dave

Reputation: 11

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

Upvotes: 1

Related Questions