【问题标题】:How to add more details in MKAnnotation in iOS如何在 iOS 的 MKAnnotation 中添加更多细节
【发布时间】:2010-02-26 13:57:08
【问题描述】:

我想在 MKAnnotation 中添加更多详细信息,例如位置标题、描述、日期、位置名称。所以需要四行。但我发现只有 2 个参数可以传递给 MKAnnotation,即标题和副标题。如何在地图上添加更多详细信息?请帮助我..提前谢谢。

【问题讨论】:

  • 我尝试添加附加到字幕的详细信息。但它显示在一行中。我如何在多行中显示详细信息

标签: iphone objective-c ios mkannotation


【解决方案1】:

看看创建一个自定义的MKAnnotationView 对象......它基本上是一个为地图注释量身定制的UIView。在该对象中,您可以拥有 4 个自定义标签。

在您的MKMapViewDelegate 类中,您实现viewForAnnotation 方法:

- (CustomMapAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    CustomMapAnnotationView *annotationView = nil;

    // determine the type of annotation, and produce the correct type of annotation view for it.
    CustomMapAnnotation* myAnnotation = (CustomMapAnnotation *)annotation;

    NSString* identifier = @"CustomMapAnnotation";
    CustomMapAnnotationView *newAnnotationView = (CustomMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if(nil == newAnnotationView) {
        newAnnotationView = [[[CustomMapAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
    }

    annotationView = newAnnotationView;
    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];

    return annotationView;
}

这将在您有注释的地方显示您的自定义视图...如果您想要分步教程,check out this video

希望对你有帮助

编辑

实际上,我刚刚在苹果开发网站上找到了一个新的Maps example... 包含您需要查看的所有源代码。他们还使用了一个名为WeatherAnnotationView的自定义MKAnnotationView

【讨论】:

  • 感谢您的回答。我可以将详细信息添加到所需格式的默认气泡中,例如换行符吗?我尝试将 UIlabel 添加到 MKPinAnnotationView 的 rightCalloutAccessoryView 中。但它不是很好。在此先感谢
  • 我不会尝试按照您尝试的方式进行操作... Apple 已设置注释,因此可以使用 MKAnnotationView 类对其进行扩展。试图以你正在做的方式覆盖 rightCalloutAccessoryView 不是一个好方法......你正在与框架作斗争,它会给你带来奇怪的结果。如果您想要自定义注释,最好的方法就是我描述的方式。
  • 本教程只更改注释图像.. 这几乎是单线
  • 在 swift 中有答案吗?
猜你喜欢
  • 1970-01-01
  • 2019-01-10
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
相关资源
最近更新 更多