开发者

MKMapKit UILabel as an annoation

开发者 https://www.devze.com 2023-02-21 04:55 出处:网络
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 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;
        CSLabelAnnotationV开发者_如何转开发iew* 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?


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

annotationView = labelAnnotationView;
0

精彩评论

暂无评论...
验证码 换一张
取 消