我正在尝试创建自定义注释,因为每个注释都保存了一些我需要在点击 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/