problem with annotation

1

when i click on an annotation, it suppose to show me that small window which is not my case, the annotations are here but when i click on one the application throw an exception, and i thought that this code make problems :

  for (int i=0; i<[array count]; i++) {

           NSDictionary *stationEnCours=[array objectAtIndex:i];


           NSString *distance=[stationEnCours objectForKey:@"distance"];
           float lng = [[stationEnCours objectForKey:@"ssiphone_longitude"]   floatValue];
           float lat = [[stationEnCours objectForKey:@"ssiphone_latitude"] floatValue];
           NSString *ensStation=[stationEnCours objectForKey:@"ssiphone_enseigne"];

           location2D = (CLLocationCoordinate2D){ .latitude = lat, .longitude = lng };
           MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease];
           [mapView addAnnotation:annotation];
           MKCoordinateSpan span={latitudeDelta:0.2,longitudeDelta:0.2};
           MKCoordinateRegion region={location2D,span};
           [mapView setRegion:region];
           [self.view addSubview:mapView];
           }

and for MyLocation class which implement the MKAnnotation protocol, i have this :

@implementation MyLocation
@synthesize enseigneDeLaStation=_enseigneDeLaStation;
@synthesize distanceVersLaStation=_distanceVersLaStation;
@synthesize coordinate=_coordinate;



-(id)initWithName:(NSString *)enseigneDeLaStation distanceVersLaStation:(NSString *) distanceVersLaStation coordinate:(CLLocationCoordinate2D)coordinate
{
    if ((self = [super init])) {
        _enseigneDeLaStation = [enseigneDeLaStation copy];
        _distanceVersLaStation = [distanceVersLaStation copy];
        _coordinate = coordinate;
    }
    return self;
}

- (NSString *)title {
    return _enseigneDeLaStation;
}

- (NSString *)subtitle {
    return _distanceVersLaStation;
}

- (void)dealloc
{
    [_enseigneDeLaStation release];
    _enseigneDeLaStation = nil;
    [_distanceVersLaStation release];
    _distanceVersLaStation = nil;    
    [super dealloc];
}       


@end

please help, thx in advance :))

EDIT @Anna : i have fix the points that you mention, but i still have the same problem :

for (int i=0; i<[array count]; i++) {
           //afficher sur la carte (map)
           NSDictionary *stationEnCours=[array objectAtIndex:i];
           //NSString *adresseStation=[stationEnCours objectForKey:@"ssiphone_adresse"];

           //float distance = [[stationEnCours objectForKey:@"distance"] floatValue];
           NSString *distance=[stationEnCours objectForKey:@"distance"];
           NSLog(@"distance: %.1f",distance);
           float lng = [[stationEnCours objectForKey:@"ssiphone_longitude"] floatValue];
           float lat = [[stationEnCours objectForKey:@"ssiphone_latitude"] floatValue];
           NSString *ensStation=[stationEnCours objectForKey:@"ssiphone_enseigne"];

           //CLLocationCoordinate2D location2D= {.latitude = lat, .longitude = lng};
           location2D = (CLLocationCoordinate2D){ .latitude = lat, .longitude = lng };
           MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease];
           [mapView addAnnotation:annotation];
          // MKCoordinateSpan span={latitudeDelta:0.2,longitudeDelta:0.2};
          // MKCoordinateRegion region={location2D,span};
           //[mapView setRegion:region];
           //[self.view addSubview:mapView];

           }
           MKCoordinateSpan span={latitudeDelta:0.2,longitudeDelta:0.2};
           MKCoordinateRegion region={location2D,span};
           [mapView setRegion:region];
           [self.view addSubview:mapView];

the Exception stack is :

2011-05-05 13:26:04.518[786:207] -[NSDecimalNumber stringByStandardizingWhitespace]: unrecognized selector sent to instance 0x6e6dca0
2011-05-05 13:26:04.520[786:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber stringByStandardizingWhitespace]: unrecognized selector sent to instance 0x6e6dca0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02915919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0272a5de objc_exception_throw + 47
    2   CoreFoundation                      0x0291742b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x02887116 ___forwarding___ + 966
    4   CoreFoundation                      0x02886cd2 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x00451608 -[UICalloutView setSubtitle:animated:] + 88
    6   UIKit                               0x004512d8 -[UICalloutView setSubtitle:] + 49
    7   MapKit                              0x023849de -[MKAnnotationContainerView _showBubbleForAnnotationView:bounce:scrollToFit:userInitiated:avoid:] + 527
    8   MapKit                              0x02385735 -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:avoid:] + 651
    9   MapKit                              0x0237fb78 -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:] + 144
    10  MapKit                              0x0235a891 -[MKMapView handleTap:] + 459
    11  UIKit                               0x005a2394 -[UIGestureRecognizer _updateGestureWithEvent:] + 727
    12  UIKit                               0x0059ebf3 -[UIGestureRecognizer _delayedUpdateGesture] + 47
    13  UIKit                               0x005a3486 _UIGestureRecognizerUpdateObserver + 637
    14  CoreFoundation                      0x028f6d1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    15  CoreFoundation                      0x0288b987 __CFRunLoopDoObservers + 295
    16  CoreFoundation                      0x02854c17 __CFRunLoopRun + 1575
    17  CoreFoundation                      0x02854280 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x028541a1 CFRunLoopRunInMode + 97
    19  GraphicsServices                    0x02e8e2c8 GSEventRunModal + 217
    20  GraphicsServices                    0x02e8e38d GSEventRun + 115
    21  UIKit                               0x0033ab58 UIApplicationMain + 1160
    22  myApp                         0x000021f8 main + 102
    23  myApp                          0x00002189 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
(gdb) 

EDIT 1 :

the two NSLogs :

NSLog(@"distance: %@", [distance class]);
           NSLog(@"ensStation: %@", [ensStation class]);

inside the for loop show me that :

2011-05-05 13:38:01.223 myApp[856:207] distance: NSCFNumber
2011-05-05 13:38:01.223 myApp[856:207] ensStation: NSCFString
2011-05-05 13:38:01.224 myApp[856:207] distance: NSDecimalNumber
2011-05-05 13:38:01.224 myApp[856:207] ensStation: NSCFString
2011-05-05 13:38:01.225 myApp[856:207] distance: NSDecimalNumber
2011-05-05 13:38:01.225 myApp[856:207] ensStation: NSCFString
2011-05-05 13:38:01.225 myApp[856:207] distance: NSDecimalNumber
2011-05-05 13:38:01.225 myApp[856:207] ensStation: NSCFString
2011-05-05 13:38:01.225 myApp[856:207] distance: NSDecimalNumber
2011-05-05 13:38:01.226 myApp[856:207] ensStation: NSCFString
2011-05-05 13:38:01.226 myApp[856:207] distance: NSCFNumber
2011-05-05 13:38:01.226 myApp[856:207] ensStation: NSCFString
2011-05-05 13:38:01.226 myApp[856:207] distance: NSDecimalNumber
2011-05-05 13:38:01.227 myApp[856:207] ensStation: NSCFString
2011-05-05 13:38:01.227 myApp[856:207] distance: NSDecimalNumber
2011-05-05 13:38:01.227 myApp[856:207] ensStation: NSCFString
(gdb) 

Edit 2 : Hi again, the problem with the annotation is fixed but i think i got the the same exception when i quit and come back to the view :

2011-05-05 14:56:47.374 TopStation[1234:207] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x806b620> was mutated while being enumerated.(
    "<MyLocation: 0x806c910>",
    "<MyLocation: 0x806c7e0>",
    "<MyLocation: 0x806c930>",
    "<MyLocation: 0x806c9d0>",
    "<MyLocation: 0x806ca20>",
    "<MyLocation: 0x806cc40>",
    "<MyLocation: 0x806c990>"
)'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02915919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0272a5de objc_exception_throw + 47
    2   CoreFoundation                      0x029153d9 __NSFastEnumerationMutationHandler + 377
    3   TopStation                          0x00005691 -[StationsSurLaCarteViewController requestFinished:] + 343
    4   TopStation                          0x00019597 -[ASIHTTPRequest reportFinished] + 171
    5   Foundation                          0x000abe9a __NSThreadPerformPerform + 251
    6   CoreFoundation                      0x028f6d7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    7   CoreFoundation                      0x028552cb __CFRunLoopDoSources0 + 571
    8   CoreFoundation                      0x028547c6 __CFRunLoopRun + 470
    9   CoreFoundation                      0x02854280 CFRunLoopRunSpecific + 208
    10  CoreFoundation                      0x028541a1 CFRunLoopRunInMode + 97
    11  GraphicsServices                    0x02e8e2c8 GSEventRunModal + 217
    12  GraphicsServices                    0x02e8e38d GSEventRun + 115
    13  UIKit                               0x0033ab58 UIApplicationMain + 1160
    14  TopStation                          0x00002188 main + 102
    15  TopStation                          0x00002119 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
(gdb) 

EDIT 3 : Hi again, i have to clean the map from annotations each time the view is Appeared so i did that (just before displaying annotations on the map :

if(request.responseStatusCode==200)
   {   

       for (id<MKAnnotation> annotation in mapView.annotations) {
          [mapView removeAnnotation:annotation];
       }
       //parse the response
           //...

so the exception was due to this for``loop, i erased it from the code and all works fine, however, i need really t clean the map from annotations each time the view is shown, is there another alternative to do so ?? thx in advance :)

ios
mapkit
mkannotation
asked on Stack Overflow May 5, 2011 by Luca • edited May 5, 2011 by Luca

1 Answer

1

The problem is this line:

NSString *distance=[stationEnCours objectForKey:@"distance"];

According to the NSLogs, the "distance" object in stationEnCours is an NSDecimalNumber (not a NSString). You then pass it to initWithAnnotation which assumes it is a NSString.

When the map view tries to display the callout bubble, it also assumes the subtitle is an NSString and tries to call stringByStandardizingWhitespace on it and fails (because it's actually an NSDecimalNumber).

Try changing the above line to:

NSString *distance = [NSString stringWithFormat:@"%f", 
    [(NSDecimalNumber *)[stationEnCours objectForKey:@"distance"] doubleValue]];
answered on Stack Overflow May 5, 2011 by (unknown user)

User contributions licensed under CC BY-SA 3.0