MKMapKit UILabel 作为注释

发布于 2024-10-28 02:08:23 字数 4465 浏览 0 评论 0原文

我认为这将是相当常见的东西 - 但在任何地方都找不到它。 我知道如何将图像放在地图上。我使用在这里找到的 CSMapAnnotation http://spitzkoff.com/craig/?p=81

我几乎以 CSMapAnnotationTypeImage 为例并制作了 CSMapAnnotationTypeLabel,但它只是在地图上显示图钉,而不是像我预期的那样显示 UILabel。

头文件

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface CSLabelAnnotationView : MKAnnotationView {
    UILabel* _label;
}
@property(retain, nonatomic)     UILabel* label;

@end

和源文件

#import "CSLabelAnnotationView.h"
#import "CSMapAnnotation.h"
#import "util.h"

@implementation CSLabelAnnotationView
@synthesize label = _label;

#define kWidth  120
#define kHeight 20

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    self.frame = CGRectMake(0, 0, kWidth, kHeight);
    self.backgroundColor = [UIColor clearColor];

    CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;   
    _label = [[UILabel alloc] initWithFrame:self.frame];
    _label.text=csAnnotation.title;
    [self addSubview:_label];
    return self;

}
#if 0
- (void)prepareForReuse
{
    TGLog(@"");
    [_imageView removeFromSuperview];
    [_imageView release];
}
#endif

-(void) dealloc
{
    [_label release];
    [super dealloc];
}
@end

将其添加到地图中

annotation = [[[CSMapAnnotation alloc] initWithCoordinate:coordinate
                                               annotationType:CSMapAnnotationTypeLabel
                                                        title:i.name
                                              reuseIdentifier:@"ocualabel"] autorelease];   
    [_mapView addAnnotation:annotation];

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

    if (annotation == mapView.userLocation){
        return nil; //default to blue dot
    }

    // determine the type of annotation, and produce the correct type of annotation view for it.
    CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
    TGLog(@"csAnnotation.annotationType %d", csAnnotation.annotationType);
    if(csAnnotation.annotationType == CSMapAnnotationTypeStart || 
       csAnnotation.annotationType == CSMapAnnotationTypeEnd)
    {
        NSString* identifier = @"Pin";
        MKPinAnnotationView* pin = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(nil == pin)
        {
            pin = [[[MKPinAnnotationView alloc] initWithAnnotation:csAnnotation reuseIdentifier:identifier] autorelease];
        }

        [pin setPinColor:(csAnnotation.annotationType == CSMapAnnotationTypeEnd) ? MKPinAnnotationColorRed : MKPinAnnotationColorGreen];

        annotationView = pin;
        TGLog(@"csPin anno");
    }
    else if(csAnnotation.annotationType == CSMapAnnotationTypeImage)
    {
        NSString* identifier = csAnnotation.reuseIdentifier;

        CSImageAnnotationView* imageAnnotationView = (CSImageAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if(nil == imageAnnotationView)
        {
            imageAnnotationView = [[[CSImageAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];   
            imageAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        TGLog(@"CSImage anno");
        annotationView = imageAnnotationView;
    } else if(csAnnotation.annotationType == CSMapAnnotationTypeLabel)
    {
        NSString* identifier = csAnnotation.reuseIdentifier;
        CSLabelAnnotationView* labelAnnotationView = (CSLabelAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if(nil == labelAnnotationView)
        {
            labelAnnotationView = [[[CSLabelAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];   
        }
        TGLog(@"CSLabel anno");

    } else {
        TGLog(@"%d", csAnnotation.annotationType);
    }
    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];
    return annotationView;      
}

有人可以帮助我吗?或者是否有其他标准方法可以在地图上获取 UILabels?

I thought this would be something that's fairly common - but cant find it anywhere.
I know how to put images on a map. I use CSMapAnnotation found here http://spitzkoff.com/craig/?p=81.

I've pretty much taken the CSMapAnnotationTypeImage as an example and made a CSMapAnnotationTypeLabel but it keep just showing pins on the map and not the UILabel like I expected.

the header file

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface CSLabelAnnotationView : MKAnnotationView {
    UILabel* _label;
}
@property(retain, nonatomic)     UILabel* label;

@end

and the source file

#import "CSLabelAnnotationView.h"
#import "CSMapAnnotation.h"
#import "util.h"

@implementation CSLabelAnnotationView
@synthesize label = _label;

#define kWidth  120
#define kHeight 20

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    self.frame = CGRectMake(0, 0, kWidth, kHeight);
    self.backgroundColor = [UIColor clearColor];

    CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;   
    _label = [[UILabel alloc] initWithFrame:self.frame];
    _label.text=csAnnotation.title;
    [self addSubview:_label];
    return self;

}
#if 0
- (void)prepareForReuse
{
    TGLog(@"");
    [_imageView removeFromSuperview];
    [_imageView release];
}
#endif

-(void) dealloc
{
    [_label release];
    [super dealloc];
}
@end

to add it to the map

annotation = [[[CSMapAnnotation alloc] initWithCoordinate:coordinate
                                               annotationType:CSMapAnnotationTypeLabel
                                                        title:i.name
                                              reuseIdentifier:@"ocualabel"] autorelease];   
    [_mapView addAnnotation:annotation];

and

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

    if (annotation == mapView.userLocation){
        return nil; //default to blue dot
    }

    // determine the type of annotation, and produce the correct type of annotation view for it.
    CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
    TGLog(@"csAnnotation.annotationType %d", csAnnotation.annotationType);
    if(csAnnotation.annotationType == CSMapAnnotationTypeStart || 
       csAnnotation.annotationType == CSMapAnnotationTypeEnd)
    {
        NSString* identifier = @"Pin";
        MKPinAnnotationView* pin = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(nil == pin)
        {
            pin = [[[MKPinAnnotationView alloc] initWithAnnotation:csAnnotation reuseIdentifier:identifier] autorelease];
        }

        [pin setPinColor:(csAnnotation.annotationType == CSMapAnnotationTypeEnd) ? MKPinAnnotationColorRed : MKPinAnnotationColorGreen];

        annotationView = pin;
        TGLog(@"csPin anno");
    }
    else if(csAnnotation.annotationType == CSMapAnnotationTypeImage)
    {
        NSString* identifier = csAnnotation.reuseIdentifier;

        CSImageAnnotationView* imageAnnotationView = (CSImageAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if(nil == imageAnnotationView)
        {
            imageAnnotationView = [[[CSImageAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];   
            imageAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        TGLog(@"CSImage anno");
        annotationView = imageAnnotationView;
    } else if(csAnnotation.annotationType == CSMapAnnotationTypeLabel)
    {
        NSString* identifier = csAnnotation.reuseIdentifier;
        CSLabelAnnotationView* labelAnnotationView = (CSLabelAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if(nil == labelAnnotationView)
        {
            labelAnnotationView = [[[CSLabelAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];   
        }
        TGLog(@"CSLabel anno");

    } else {
        TGLog(@"%d", csAnnotation.annotationType);
    }
    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];
    return annotationView;      
}

Can anyone help me with this? Or is there some other standard way to get UILabels on a map?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缪败 2024-11-04 02:08:23

找到了。
需要在 viewForAnnotation() 中添加以下内容:

annotationView = labelAnnotationView;

Found it.
Needed to add this in viewForAnnotation():

annotationView = labelAnnotationView;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文