Does the MKAnnotationView buffer its input queue?

I want to display different colored contacts in a UIMapView based on the relative time that they represent. but it looks like the mapView: viewForAnnotation: method does this regardless of when it was called.

In my code example, I already loaded earlier and newer locations from the file into self.fileArray. the array contains objects called pins that have (in particular) an age property. the latest results start at age @ "0", and every time the array is reloaded, it is ready to accept new results, they progress to the age of "1" and "2", respectively, after which they are then discarded.

As soon as they take their new age property, they are sent to mapView: viewForAnnotation: method to display according to their new status when I repeat the fileArray file

The actual question is after the jump. Many other interesting answers arose in formulating the question, but no one applied to my case.

.
.
int size = [self.fileArray count];
for (int idx=(size-1); idx>0; idx--)  // process backwards
    {
        annotationFlag = 0;           // using a global just for now
    self.finding = self.fileArray[idx];
        if ([self.finding.age isEqualToString:@"2"]) {
            [self.fileArray removeObjectAtIndex:idx];
        }

        if ([self.finding.age isEqualToString:@"1"]) {
            self.finding.age = @"2";
            [self.fileArray replaceObjectAtIndex:idx withObject:self.finding];
            annotationFlag = 2;

            // tried here , only displays the newest
        }

        if ([self.finding.age isEqualToString:@"0"]) {
            self.finding.age = @"1";
            [self.fileArray replaceObjectAtIndex:idx withObject:self.finding];
            annotationFlag = 1;

            // tried here, still only displays the same newest 
        }

    }   // end if

//<Breakpoint with Log here> 

    MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
    CLLocationCoordinate2D myCoordinate;
    myCoordinate.latitude =[self.finding.myLat doubleValue];
    myCoordinate.longitude=[self.finding.myLong doubleValue];
    annotation.coordinate = myCoordinate;
    [self.mapView addAnnotation:annotation];
}       // end for 
.
.

annotation methods are pretty standard, as used by most of all:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation: (MKUserLocation *)userLocation {
_mapView.centerCoordinate = userLocation.location.coordinate;
}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

if([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

static NSString *identifier = @"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[ self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
    annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

//<Breakpoint with Log here>

    switch (annotationFlag) {
        case 1:
        annotationView.pinColor = MKPinAnnotationColorGreen;
            break;
        case 2:
        annotationView.pinColor = MKPinAnnotationColorRed;
            break;
        default:
        annotationView.pinColor = MKPinAnnotationColorPurple;
        break;
    }
    annotationView.animatesDrop = YES;
    annotationView.canShowCallout = NO;
}else {
    annotationView.annotation = annotation;
}
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeSystem]; // UIButtonTypeDetailDisclosure
return annotationView;
}

also under the test of my curious neighbors lovers. pins should show different colors for each run<screenshot>

If I NSLog the Flag annotations for the console at different points on mapView: viewForAnnotation: it seems to ignore the values ​​in the annotationFlag and use only the last state, making me think that it only works when the for loop is complete and does not follow the iterations.

: [self.mapView addAnnotation: annotation]. Ive for, .

: log-to-console, , 42 ( , ), , , 42 .

mapView: viewForAnnotation, 42 , 43- . , , . .

+1
1

, viewForAnnotation addAnnotation .

, , , . , .

- .

annotation, , . - , .

, , .:

:

  • MKPointAnnotation, , viewForAnnotation ( , , ).

    MKPointAnnotation Finding ( self.finding) MKAnnotation. SO.

    annotationFlag MKPointAnnotation Finding ( "" ) addAnnotation.

  • viewForAnnotation pinColor if-else, / return. pinColor age annotation, ( Finding). :

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation 
    {
        if([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
    
        static NSString *identifier = @"myAnnotation";
        MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (!annotationView)
        {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.animatesDrop = YES;
            annotationView.canShowCallout = NO;
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeSystem];
        }else {
            annotationView.annotation = annotation;
        }
    
        //update the pinColor in the view whether it a new OR dequeued view...
        if ([annotation isKindOfClass:[Finding class]])
        {
            Finding *f = (Finding *)annotation;
    
            if ([f.age isEqualToString:@"2"]) {
                annotationView.pinColor = MKPinAnnotationColorGreen;
            }
            else if ([f.age isEqualToString:@"1"]) {
                annotationView.pinColor = MKPinAnnotationColorPurple;
            }
            else {
                annotationView.pinColor = MKPinAnnotationColorRed;
            }
        }
    
        return annotationView;
    }
    
+1

Source: https://habr.com/ru/post/1547435/


All Articles