MKMapview 改变标注图片

MKMapview change annotation image

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id<MKAnnotation>)annotation {
    NSLog(@"annotion");

    CGSize imgSize;
    MKAnnotationView *pinView = nil;
    static NSString *defaultPinID = @"pin";
    pinView = (MKAnnotationView *)
    [self.m_mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
    pinView.canShowCallout = NO;
    pinView.image = [UIImage imageName:@"blue.jpg"]; 
    return pinView;
}    


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    view.image = [UIImage imageName:@"orange.jpg"]
}

我在带有图像(即蓝色图像)的 mapView 上添加了注释。当我点击注释时,我更改了图像(即橙色图像)。然后我点击另一个注释更改注释(i.e.orange 图像)到(即蓝色图像)并将所选注释更改为(i.e.orange 图像)。任何帮助将提前 appricated.thanks

像这样尝试,可能会有帮助。

重新加载所有其他注释。

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView (MKAnnotationView *)view{
     for (id<MKAnnotation> annotation in mapView.annotations){
        MKAnnotationView* anView = [mapView viewForAnnotation: annotation];
        if (anView){
            anView.image = [UIImage imageNamed:@"blue.jpg"]; 
        }
     }
     view.image = [UIImage imageName:@"orange.jpg"];
}

试试这个

对于 swift 3.0

for annotation: MKAnnotation in mapView.annotations { 

    let anView = mapView.view(for: annotation)

    if (anView != nil) 
    {
        anView?.image = UIImage(named: "home_pin")
    }
}

view.image = UIImage(named: "home_pin01")