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--)
{
annotationFlag = 0;
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;
}
if ([self.finding.age isEqualToString:@"0"]) {
self.finding.age = @"1";
[self.fileArray replaceObjectAtIndex:idx withObject:self.finding];
annotationFlag = 1;
}
}
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];
}
.
.
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];
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];
return annotationView;
}
also under the test of my curious neighbors lovers. pins should show different colors for each run
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- . , , . .