简体   繁体   中英

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

I added annotations on mapView with image(ie blue image).When i tap a annotation i changed image (ie orange image). then i tap on another annotation change annotation(ieorange image) to (ie blue image) and change selected annotation to (ieorange image).any help will be appricated.thanks in advance

Try like this, may be helpful.

Reload all other annotations.

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

Try this

for 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM