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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2342070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:58:54  来源:igfitidea点击:

How to add more details in MKAnnotation in iOS

iphoneobjective-ciosmkannotation

提问by S.P.

I want to add more details in MKAnnotation like location title, description, date, location name. So it will be four lines that are needed. But I found that only 2 parameters can be passed to MKAnnotation which are title and subtitle. How can I add more details on the map? Plz help me..Thanks in advance.

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

采纳答案by Ryan Ferretti

Take a look at creating a custom MKAnnotationViewobject... it is basically a UIViewthat is tailored for map annotations. In that object you could have your 4 custom labels.

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

In your MKMapViewDelegateclass, you implement the viewForAnnotationmethod:

在您的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;
}

And that will display your custom view where ever you have an annotation... if you want a step by step tutorial, check out this video.

这将在您有注释的地方显示您的自定义视图……如果您想要分步教程,请查看此视频

hope this helps

希望这可以帮助

EDIT

编辑

I actually just found a new Maps exampleon the apple dev site... has all the source code you need to go through. They are also using a custom MKAnnotationViewcalled WeatherAnnotationView

我实际上刚刚在苹果开发网站上找到了一个新的地图示例......拥有您需要通过的所有源代码。他们还使用了一个MKAnnotationView名为WeatherAnnotationView