3

I set the mapView.delegate, etc.
Everything is ok but when I leave and few minutes after I resume my app, it quits.

The console gives me this but I haven't been able to fix it:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'Invalid Region <center:-180.00000000, -180.00000000 span:+0.09000000, +0.09000000>'

Send setRegion in didUpdateUserLocation method after userLocation was updated

 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [mapView removeAnnotations: [mapView annotations]];
    for (TOJSearchItem *searchItem in _searchItems) {
        TOJPlaceRequest *request = [TOJPlaceRequest new];
        [request execute:SERVER_URL coordinate:userLocation.coordinate searchItem:searchItem delegate:self];
    }
    MKCoordinateRegion region = {{userLocation.coordinate.latitude , userLocation.coordinate.longitude}, {0.09f , 0.09f}};
    [mapView setRegion:region animated: YES];
}

make request

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (_lastLocation != nil) {
        CLLocationDistance meters = [newLocation distanceFromLocation:_lastLocation];
        if (meters < DISTANCE_MINI) {
//            CALCUL tout les points par rapport a current location
//            NSLog(@"%f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
        } else {
            for (TOJSearchItem *searchItem in _searchItems) {
                TOJPlaceRequest *request = [TOJPlaceRequest new];
                [request execute:SERVER_URL coordinate:newLocation.coordinate searchItem:searchItem delegate:self];
            }
            MKCoordinateRegion region = {{newLocation.coordinate.latitude , newLocation.coordinate.longitude}, {0.09f , 0.09f}};
            [_mapView setRegion: region animated: YES];
            NSLog(@"reconnection");
        }
    }
    _lastLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
}

reset the region after clicked on an annotation

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    if ([view class] == TOJClusterPushPinView.class) {
        TOJClusterPushPinView *clusterPushPinView = (TOJClusterPushPinView *)view;
        CLLocationCoordinate2D topLeftCoordinate;
        topLeftCoordinate.latitude = -90;
        topLeftCoordinate.longitude = 180;
        CLLocationCoordinate2D bottomRightCoordinate;
        bottomRightCoordinate.latitude = 90;
        bottomRightCoordinate.longitude = -180;
        for (TOJPushPin *pushPin in clusterPushPinView.clusterPushPin.pushPins) {
            topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, pushPin.coordinate.longitude);
            topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, pushPin.coordinate.latitude);
            bottomRightCoordinate.longitude = fmax(bottomRightCoordinate.longitude, pushPin.coordinate.longitude);
            bottomRightCoordinate.latitude = fmin(bottomRightCoordinate.latitude, pushPin.coordinate.latitude);
        }
        MKCoordinateRegion region;
        region.center.latitude = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 0.5;
        region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 0.5;
        region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 2.0; // Add a little extra space on the sides
        region.span.longitudeDelta = fabs(bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 2.0; // Add a little extra space on the sides
        region = [mapView regionThatFits:region];
        [mapView setRegion:region animated:YES];
    }
}
james075
  • 1,280
  • 1
  • 12
  • 22
  • What does "when I leave" mean exactly (pop vc, dismiss vc, other)? Show the code where it calls setRegion. –  Oct 14 '12 at 22:28
  • when i switch app (whatever ap) and i back to this app, it quits and send me error on console – james075 Oct 15 '12 at 00:30
  • Try applying the suggestions for [this question](http://stackoverflow.com/questions/8394969/unrecognized-error-in-mapview-ios). Also see ng32rg's answer to that question. The most useful check in your case might be `if (!CLLocationCoordinate2DIsValid(center))`. –  Oct 15 '12 at 01:49

3 Answers3

0

If your didUpdateToLocation method you should check that the location variable you are being given is valid.

 if (userLocation.location.horizontalAccuracy >= 0) {

It is known behaviour, though I don't see the logic in it, that the first location it gets after a resume may be invalid. Any location with an accuracy < 0 should be discarded.

Ref: http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#//apple_ref/occ/instp/CLLocation/horizontalAccuracy

Craig
  • 8,093
  • 8
  • 42
  • 74
  • Have you put in any breakpoints or log statements to see if it is didUpdateUserLocation or didUpdateToLocation? I see in your answer (why are you answering your own question to say that the answer you're showing doesn't work?) only shows a change to didUpdateToLocation. – Craig Oct 15 '12 at 22:16
  • i put the condition check and a break point on didUpdateUserLocation, here is the problem it Terminating on this line : MKCoordinateRegion region = {{userLocation.coordinate.latitude , userLocation.coordinate.longitude}, {0.09f , 0.09f}}; [mapView setRegion:region animated: YES]; – james075 Oct 15 '12 at 22:36
  • And did you try the horizontalAccuracy check in didUpdateUserLocation? – Craig Oct 15 '12 at 23:22
  • yes i did, it doesnt work too, i really dont understand why it doesnt works – james075 Oct 15 '12 at 23:54
  • It does seem very strange that you're getting the lat and long as -180. Could you try printing out the value of the lat/long as the first line of the function, and then again just before you make that region. It could be that your [request execute: search....] is modifying the values. – Craig Oct 16 '12 at 00:10
  • start app : userlocation.coord - > 48.624554, 2.467964 resume app : userlocation.coord - > -180.000000, -180.000000 In [request...] i dont modifying values, just send a request to the server for annotations. lat = [[[venue objectForKey:@"location"] objectForKey: @"lat"] floatValue]; long = [[[venue objectForKey:@"location"] objectForKey: @"lng"] floatValue]; MKCoordinateRegion region = {{lat, long}, {0.009f, 0.009f}}; pu *pushPin = [[TOJPushPin alloc] initWithTitle:_searchItem.type coordinate:region.center address:address city:city]; [_searchItem.pushPins addObject:pushPin]; – james075 Oct 16 '12 at 00:47
0

change to

 if (userLocation.location.horizontalAccuracy >= 1) {

 .......

 }

it works

Kalaichelvan
  • 220
  • 3
  • 11
0

You should check in didUpdateUserLocation if

userLocation.location != nil

In my application, when returning from an applicationDidBecomeActive event, the first coordinate is always invalid.