MKAnnotation pin color update

My application displays a table view that contains the result of a user search from a database. Now in this tabular view there is one button by clicking on this user to see the location of all the users who are searching on the map. When the user first clicks on the button, he will be able to see the entire user's location with a green color. I show the map in half of the review. Now that the user selects a specific user from the table, I want to change the color of this user pin. And if the user selects any other user from the table, the previous selected icon color of the user must be changed as it was before. I tried this:

- (void)selectAnnotation:(id < MKAnnotation >)annotation animated:(BOOL)animated{
    MKPinAnnotationView *chng=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    chng.pinColor=MKPinAnnotationColorRed;
    NSLog(@"========>selected");
 }

And I call this method when the user selects a specific user from the table.

CLLocationCoordinate2D CurrentCoordinateSingleUser;
CurrentCoordinateSingleUser.latitude=[[singleUserPin objectAtIndex:1] doubleValue];
CurrentCoordinateSingleUser.longitude=[[singleUserPin objectAtIndex:2] doubleValue];
MapObjects   *map1=[[MapObjects alloc]initwithCoordinate:CurrentCoordinateSingleUser title:[singleUserPin objectAtIndex:0] subtitle:Nil];

[userMap addAnnotation:map1];
for (id<MKAnnotation> currentAnnotation in userMap.annotations) {       
    if ([currentAnnotation isEqual:annotationToSelect]) {
        [userMap selectAnnotation:currentAnnotation animated:FALSE];
    }
} 

But it selectAnnotationdoesn’t work here .

UPDATE ::

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

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


    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[userMap dequeueReusableAnnotationViewWithIdentifier:identifier];
    if([annotation isEqual:map1]){
        annotationView.pinColor = MKPinAnnotationColorRed;

    }
    else {
        if (!annotationView)
        {

            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
//            if(fromSelectedTab==TRUE){
//            annotationView.pinColor = MKPinAnnotationColorRed;
//            annotationView.animatesDrop = NO;
//            fromSelectedTab=FALSE;
//            }
//            else{
            annotationView.pinColor = MKPinAnnotationColorGreen;
            annotationView.animatesDrop = NO;
//            }
            annotationView.canShowCallout = YES;

                // annotationView.dr
        }
        else {
            annotationView.annotation = annotation;
        }
    }
    //annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}

MapObjects.h:

@interface MapObjects : NSObject<MKMapViewDelegate,MKAnnotation>
{
    NSString *title,*subtitle;
    CLLocationCoordinate2D coordinate;
}
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *subtitle;
@property(nonatomic) CLLocationCoordinate2D coordinate;
-(MapObjects *)initwithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString    *)title subtitle:(NSString *)subtitle;

@end
+3
source share
3 answers

Using:

- (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)annotation;

instead of creating a new one MKPinAnnotationView. Or, if necessary, you need to add somewhere new annotationon your map.

Hope that helps

0
source

You must override the MKMapiew method

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

       static NSString *identifier = @"PinAnnotation";
       // map1 should be visible in method context  
       if ([annotation isEqual:map1]) {
           MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
           pinAnnotation.pinColor = MKPinAnnotationColorRed;// or any you want
           return pinAnnotation;
       }
       return nil;
}

Here you can define a given annotation and set the desired color.

0
source

in viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    // There you should have selected "user" object and "users" array

    ...

    // _annotations is declared in header
    _annotations = [[NSMutableArray alloc] initWithCapacity:0];

    for (UserObj *obj in users) {
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(0.f, 0.f);
        coordinate.latitude = obj.latitude;
        coordinate.longitude = obj.longitude;
        MapObjects *userAnnotation = [[MapObjects alloc] initWithCoordinate:coordinate title:obj.name subtitle:nil];
        [map addAnnotation:userAnnotation];
        [_annotations addObject:userAnnotation];
    }

    ...
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view {
    view.pinColor = MKPinAnnotationColorRed;
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view {        
    view.pinColor = MKPinAnnotationColorGreen;
}

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

    static NSString *identifier = @"PinAnnotation";
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if(!pinAnnotation) {
        pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    }
    pinAnnotation.pinColor = MKPinAnnotationColorGreen;

    return pinAnnotation;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // You should keep your annotation objects in some array too because map.annotations won't return annotation in order that we need
    // After you add annotation to map, add them to NSMutableArray too, [_annotations addObject:userAnnotation];
    MapObjects annotation = _annotations[indexPath.row];// not sure that it return that exact annotation
    [map selectAnnotation:annotation animated:YES];
}
0
source

All Articles