2011-03-30 6 views
2

나는 이것이 매우 일반적이라고 생각했지만 어디서나 찾을 수 없다. 지도에 이미지를 넣는 방법을 알고 있습니다. 여기 CSMapAnnotation을 사용합니다 (http://spitzkoff.com/craig/?p=81).MKMapKit UILabel을 annoation으로 사용

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를지도에 표시하는 다른 표준 방법이 있습니까?

답변

1

발견.
viewForAnnotation()

annotationView = labelAnnotationView; 
관련 문제