Crash on replacing map annotations

Posted by Alwin on Stack Overflow See other posts from Stack Overflow or by Alwin
Published on 2010-06-17T11:06:19Z Indexed on 2010/06/17 15:43 UTC
Read the original article Hit count: 312

Solved it, see below code

I'm trying to replace annotations on my MapView depending on the distance between user location and annotation. The annotations are getting replaced like they should, but when I touch te mapview my app crashes. This is de code I have so far:

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];

for (id object in self.mapAnnotations) {

    if([object isKindOfClass:[ClosedAnnotation class]])
    {

        ClosedAnnotation *ann = (ClosedAnnotation *)object;
        CLLocation *tempLocation = [[CLLocation alloc] initWithLatitude:ann.coordinate.latitude longitude:ann.coordinate.longitude];
        double distance = [self.currentLocation getDistanceFrom: tempLocation] / 1000;
        [tempLocation release];

        if(distance <= 1.0){
            [mapView removeAnnotation:object];
            OpenAnnotation *openAnnotation = [[OpenAnnotation alloc] initWithLatitude:ann.coordinate.latitude
                                                                            longitude:ann.coordinate.longitude
                                                                             imageSrc:[ann getImageSrcForAnnotation]
                                                                                title:ann.title
                                                                                tekst:[ann getTextForAnnotation]
                                                                            imageSize:[ann getSizeForImage]
                                                                             username:[ann getUserNameForAnnotation]
                                                                             usertext:[ann getUserTextForAnnotation]];
            [mapView addAnnotation:openAnnotation];
            [tempArray addObject:openAnnotation];
            [openAnnotation release];

        } else {
            [tempArray addObject:object];
        }

    } 
    else if([object isKindOfClass:[OpenAnnotation class]]){
        OpenAnnotation *ann = (OpenAnnotation *)object;
        CLLocation *tempLocation = [[[CLLocation alloc] initWithLatitude:ann.coordinate.latitude longitude:ann.coordinate.longitude] autorelease];
        double distance = [self.currentLocation getDistanceFrom: tempLocation] / 1000;
        [tempLocation release];

        if(distance > 1.0){
            [mapView removeAnnotation:object];
            ClosedAnnotation *closedAnnotation = [[ClosedAnnotation alloc] initWithLatitude:ann.coordinate.latitude
                                                                                  longitude:ann.coordinate.longitude
                                                                                   imageSrc:[ann getImageSrcForAnnotation]
                                                                                      title:ann.title
                                                                                      tekst:[ann getTextForAnnotation]
                                                                                  imageSize:[ann getSizeForImage]
                                                                                   username:[ann getUserNameForAnnotation]
                                                                                   usertext:[ann getUserTextForAnnotation]];
            [mapView addAnnotation:closedAnnotation];
            [tempArray addObject:closedAnnotation];
            [closedAnnotation release];

        } else {
            [tempArray addObject:object];
        }
    }

}

[self.mapAnnotations removeAllObjects];
[self.mapAnnotations addObjectsFromArray:tempArray];
[tempArray release];

I solved it by getting rid of the complete "two-class"-structure and handling everything within one annotation class. Works like a charm now.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c