ios - 没有设置图像,自定义注释不会出现

标签 ios objective-c mapkit

我正在尝试创建自定义注释,因为每个注释都保存了一些我需要在点击 pin 时显示的数据。但我需要显示默认的 ios pin 图像。一切正常,但 pin 只会在我设置任何图像时显示。如果不设置图像,它不会显示默认的 ios 引脚。我的代码是
CustomAnnotation.h文件

    @interface CustomAnnotation : NSObject<MKAnnotation>
    @property(nonatomic,strong)NSString *title;
    @property(nonatomic,strong)NSString *subTitle;
    @property(nonatomic)CLLocationCoordinate2D coordinate;
    @property(nonatomic,strong) UserDevice *userData;
    -(instancetype)initWithUserData:(UserDevice *)userData;

在 mapViewC.h 上

-(void)addMarkersOnMap{
for (UserDevice* userDevice in markersArray) {
    CustomAnnotation *point = [[CustomAnnotation alloc]initWithUserData:userDevice];
    point.coordinate = CLLocationCoordinate2DMake(userDevice.latitude, userDevice.longitude);
    point.userData = userDevice;
    point.title = @"dszf";
    [mapview addAnnotation:point];       
    MKMapRect mapRect = [self getZoomingRectOnMap:mapview toFitAllOverlays:YES andAnnotations:YES includeUserLocation:NO];
    [mapview setVisibleMapRect:mapRect edgePadding:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) animated:YES];
}

}
#pragma mark - MKMapView Delegate methods

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;
}
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
    CustomAnnotation *customAnnotation = annotation;
    MKAnnotationView *customAnnotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annotationViewID"];
    if (customAnnotationView == nil){
        customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotationViewID"];
    }
    //        customAnnotationView.image = [UIImage imageNamed:@"pin-1"];
    customAnnotationView.canShowCallout = true;
    customAnnotationView.annotation = customAnnotation;
    return customAnnotationView;
}
return nil;
}

最佳答案

viewForAnnotation MKMapViewDelegate 方法中使用 MKPinAnnotationView 而不是 CustomAnnotationView

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    if ([annotation isKindOfClass:[CustomAnnotation class]]) {
        CustomAnnotation *customAnnotation = annotation;
        MKPinAnnotationView *customAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annotationViewID"];
        if (customAnnotationView == nil){
            customAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotationViewID"];
        }
        //        customAnnotationView.image = [UIImage imageNamed:@"pin-1"];
        customAnnotationView.canShowCallout = true;
        customAnnotationView.annotation = customAnnotation;
        return customAnnotationView;
    }
    return nil;
}

希望对你有帮助

关于ios - 没有设置图像,自定义注释不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45861197/

相关文章:

iphone - 遇到 NSUserDefault 问题

objective-c - Objective-C - 错误 : 'Expected a type'

ios - 获取经度和纬度

ios - 将NSMutableArray保存到NSUserDefaults

iOS 8 自定义键盘 UIScrollview 不工作

ios - 在后台运行 ios 应用程序

ios - MapKit 动画

ios - 可以使用 AudioUnit (CoreAudio) 将音频编码为 AAC 或 MP3 吗?

android - 为什么 React Native WebView 会转到 :blank in release build? 左右

swift - 在 map 上将注释固定为 subview