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];
}
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?
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:
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With