我正在尝试创建一个看起来像这样的自定义地图标注:
如您所见,我需要自定义引脚和自定义标注。引脚的颜色基于等级,以及标注中圆圈的颜色。
这是我到目前为止的代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[CustomAnnotation class]])
{
static NSString *identifier = @"CustomAnnotation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
annotationView.animatesDrop = NO;
}
else
{
annotationView.annotation = annotation;
}
CustomAnnotation *myAnnotation= (CustomAnnotation *)annotation;
// Based on the score, set the pin image
if ([myAnnotation.score intValue] == 1)
{
annotationView.image = [UIImage imageNamed:@"dot_purple_xsmall.png"];
}
else if (...)
{
// and so on...
}
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if (![view.annotation isKindOfClass:[MKUserLocation class]])
{
CustomCalloutView *callout = CustomCalloutView *)[[[NSBundle mainBundle]loadNibNamed:@"MyCallout" owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = callout.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width / 2 + 15, -calloutViewFrame.size.height);
callout.frame = calloutViewFrame;
// TODO: Set the outlets of the xib
callout.nameLabel.text = @"Testing";
[view addSubview:callout];
}
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
for (UIView *subView in view.subviews)
{
[subView removeFromSuperview];
}
}
自定义引脚工作正常但是当我点击一个引脚时,我得到的只是一个黑色视图。该应用程序还使用导航和选项卡控制器。