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

S.P*_*.P. 20 iphone objective-c mkannotation ios

我想在MKAnnotation中添加更多细节,如位置标题,描述,日期,位置名称.所以需要四条线.但我发现只有2个参数可以传递给MKAnnotation,它们是标题和副标题.如何在地图上添加更多详细信息?Plz帮帮我......谢谢.

Rya*_*tti 15

看看创建一个自定义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;
}
Run Code Online (Sandbox Code Playgroud)

这将显示您的自定义视图,只要您有注释...如果您想要一步一步的教程,请查看此视频.

希望这可以帮助

编辑

我实际上刚刚在apple dev网站上找到了一个新的地图示例 ...包含了您需要完成的所有源代码.他们也使用自定义MKAnnotationView调用WeatherAnnotationView

  • 本教程只更改注释图像..这几乎是一个单行 (2认同)