MKMapView 更改标注图像

3
- (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"]
}

我在地图视图上添加了带有图片(如蓝色图片)的注释。当我点击一个注释时,我会更改图片(即橙色图片)。然后我点击另一个注释,将注释的图片(即橙色图片)更改为蓝色图片,并将选定的注释更改为橙色图片。任何帮助都将不胜感激。提前致谢。

2个回答

4

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

重新加载所有其他注释。

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

试一下这个


3

针对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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接