MKAnnotation引脚颜色更新

时间:2014-02-24 10:03:28

标签: ios mkmapview mkannotation mkannotationview

在我的应用程序中,我正在显示一个tableview,其中包含从数据库中搜索到的用户的结果。现在在该tableview中有一个按钮,通过点击该用户可以在地图中查看所有搜索到的用户的位置。当用户第一次点击按钮时,他将能够看到所有用户的绿色针位置。我在视图的一半显示地图。现在当用户从表中选择任何特定的搜索用户时,我想要更改该用户的pin的颜色。如果用户从表中选择任何其他用户,之前选择的用户的引脚颜色应该改变为之前的状态。 我试过这个:

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

当用户从表中选择特定用户时,我正在调用此方法。

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];
    }
} 

但是这里selectAnnotation无效。

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 个答案:

答案 0 :(得分:0)

使用:

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

而不是创建新的MKPinAnnotationView。或者,如果它有目的,您必须在地图中添加新的annotation

希望有所帮助

答案 1 :(得分:0)

您应该覆盖MKMapiew方法

- (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;
}

在那里,您可以确定指定的注释并设置所需的颜色。

答案 2 :(得分:0)

在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];
}