如果没有设置图像,则不会显示自定义注释

时间:2017-08-24 12:06:25

标签: ios objective-c mapkit

我正在尝试创建自定义注释,因为每个注释都会保存一些我需要在引脚上显示的数据。但我需要显示默认的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;
}

2 个答案:

答案 0 :(得分:1)

而不是CustomAnnotationViewMKPinAnnotationView viewForAnnotation方法中使用MKMapViewDelegate

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

希望这有帮助

答案 1 :(得分:0)

  CustomAnnotation *customAnnotation = annotation;
  NSString *annotationIdentifier = @"PinViewAnnotation"; 
  MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier: annotationIdentifier]; 

  if (pinView ==nil) {
        pinView = [[MKPinAnnotationView alloc]initWithAnnotation: customAnnotation reuseIdentifier: annotationIdentifier];

    }
   pinView.pinColor = MKPinAnnotationColorGreen; // change color
   pinView.canShowCallout = YES;
   pinView.enabled = YES;
   mapView selectAnnotation:pinView animated:YES];