具有自定义MKAnnotation的MKMapView [英] MKMapView with custom MKAnnotation

查看:94
本文介绍了具有自定义MKAnnotation的MKMapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MKMapView. 我想在地图上添加自定义的MKA注释.

I've a MKMapView. I want to put a custom MKAnnotation on my map.

它们是一些餐馆的地方.我该怎么办?

They are some restaurant places. How can I do it?

我的问题是如何制作自定义MKA注释?

My question is how can I make a custom MKAnnotation?

谢谢,伙计们.

推荐答案

首先,让我们将custom定义为含义,而不仅仅是标题和副标题.我们要更改MKAnnotation的大小,并包括一些自定义图形.

First, let's define custom as meaning not simply title and subtitle. We want to change the size of the MKAnnotation and include some custom graphics.

您可能需要自定义注释的两个部分:

There are two parts to an annotation you might want to customize:

  • MKA注释
  • MKAnnotationView

对于最基本的MKA注释,您只需采用该协议并为标题和副标题返回nil,但是在点击附件指示器时,您还可以在注释中包含更多信息以扩展标注.例如,您可以使用viewDidLoad中的addAnnotation:将所有注释添加到MKMapView.

For the most basic MKAnnotation you would simply adopt the protocol and return nil for title and subtitle, but you could also carry a lot more information in your annotation for an extended callout upon tapping an accessory indicator. You can add all of the annotations to the MKMapView using addAnnotation: in viewDidLoad for example.

@interface CPAnnotation : NSObject <MKAnnotation> {
@private
    CLLocationCoordinate2D _coordinate;
    NSString *_title;
    NSString *_subtitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end

MKA注释实现

@implementation CPAnnotation
@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
    self = [super init];

    if (self != nil) {
        self.coordinate = coordinate;
    }

    return self;
}

- (NSString *)title {
    return _title;
}

- (NSString *)subtitle {
    return _subtitle;
}
@end

下一步是从放置的引脚上自定义标注.为此,您需要自定义MKAnnotationView.根据苹果公司的说法,默认情况下您不应该进行大量标注.他们建议使用一个标准尺寸的标注,该标注带有一个可以打开较大的标注的按钮.他们在蓝色圆圈图标中使用小写字母i.可以通过视图的leftCalloutAccessoryView和rightCalloutAccessoryView属性设置这些图标.如果您已经采用了MKMapViewDelegate协议并将自己设置为MKMapView的委托,则将获得viewForAnnotation:的回调.

The next step is to customize the callout from the pin dropped. To do this you need to customize MKAnnotationView. According to Apple you shouldn't make a huge callout by default. They recommend a standard size callout that has a button to open a bigger one. They use the lowercase i in a blue circle icon. Those icons can be set via the view's leftCalloutAccessoryView and rightCalloutAccessoryView property. If you already adopted the MKMapViewDelegate protocol and set yourself as the MKMapView's delegate you will get the callback for viewForAnnotation:.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *const kAnnotationReuseIdentifier = @"CPAnnotationView";

MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kAnnotationReuseIdentifier];
if (annotationView == nil) {
    annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kAnnotationReuseIdentifier] autorelease];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];
}

return annotationView;
}

您可以在覆盖drawRect方法的自定义视图中进一步自定义此视图,为image属性提供图像,或者甚至可以在XIB中实现MKAnnotationView.值得一试.

You can further customize this in a custom view overriding the drawRect method, providing an image to the image property, or you could even implement an MKAnnotationView in a XIB. It is worth some experimentation.

Apple的WeatherAnnotationView示例说明了重写的drawRect.

Apple's WeatherAnnotationView Example illustrates overriding drawRect.

这篇关于具有自定义MKAnnotation的MKMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆