iphone - 如何在 iOS 的 MKAnnotation 中添加更多细节

标签 iphone objective-c ios mkannotation

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

最佳答案

看看创建自定义 MKAnnotationView 对象...它基本上是一个为 map 注释量身定制的 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;
}

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

希望对你有帮助

编辑

实际上我刚刚发现了一个新的 Maps example在 apple dev 站点上... 拥有您需要查看的所有源代码。他们还使用名为 WeatherAnnotationView

的自定义 MKAnnotationView

关于iphone - 如何在 iOS 的 MKAnnotation 中添加更多细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2342070/

相关文章:

ios - iphone主题处于黑暗模式时导航栏样式更改

ios - 了解自动约束的设置位置

iphone - 使用 SVGKit 加载 SVG 文件

iphone - 如何在 Objective-C 中的 View Controller 之间共享通用方法?

objective-c - 在 Objective C 中格式化字符串

iphone - NSData 附加数据 : UIImageJPEGRepresentation() returns nil

ios - 没有互联网连接时调用 "didFailLoadWithError"?

iphone - 带有部分粗体文本的自定义 UITableViewCell

iphone - 如何使用 Core Graphics/iPhone 绘制渐变线(淡入/淡出)?

ios - 引用 block 内的实例变量