如何通过Objective-C添加自定义图像以替换iOS MKMapView中的默认引脚



所以到目前为止,我只有默认的MKMapView引脚工作。但是我想要一个自定义图像来替换那个小红色别针。这是我在下面看到的。我错过了什么?我认为定义 view.image 可以解决问题,但我想不会。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:   
  (id<MKAnnotation>)annotation {
    MKPinAnnotationView *view = [[MKPinAnnotationView alloc]  
    initWithAnnotation:annotation reuseIdentifier:@"pin"];
    view.enabled = YES;
    view.animatesDrop = YES;
    view.canShowCallout = YES;
    view.image = [UIImage imageNamed:@"custom-pin-image.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage         
     imageNamed:@"custom-image-for-annotation.png"]];
    view.leftCalloutAccessoryView = imageView;
    view.rightCalloutAccessoryView = [UIButton buttonWithType:  
     UIButtonTypeDetailDisclosure];
    return view;
}

MKPinAnnotationViewMKAnnotationView的子类,它具有image属性,但它忽略了它,而是显示默认的引脚图像。

您应该改用MKAnnotationView

此外,请确保您的MKMapView代理未被激活。

最新更新