我正在尝试创建自定义注释,因为每个注释都会保存一些我需要在引脚上显示的数据。但我需要显示默认的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;
答案 0 :(得分:1)
而不是CustomAnnotationView
在MKPinAnnotationView
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];