2011-03-30 3 views
2

Je pensais que ce serait quelque chose qui est assez commun - mais ne peux pas le trouver n'importe où. Je sais comment mettre des images sur une carte. J'utilise CSMapAnnotation trouvée ici http://spitzkoff.com/craig/?p=81.MKMapKit UILabel comme une annotation

J'ai pris à peu près le CSMapAnnotationTypeImage comme exemple et fait un CSMapAnnotationTypeLabel mais il montre juste des épingles sur la carte et pas le UILabel comme je m'y attendais.

le fichier d'en-tête

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


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

@end 

et le fichier source

#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 

pour l'ajouter à la carte

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

et

- (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;  
} 

Quelqu'un peut-il m'aider avec cela? Ou existe-t-il un autre moyen standard d'obtenir UILabels sur une carte?

Répondre

1

Trouvé.
Nécessaire pour ajouter à viewForAnnotation():

annotationView = labelAnnotationView;