2013-10-24 103 views
3

的形象這是我怎麼就MapKit添加註釋:更改註釋銷

MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 

    point.coordinate = zoomLocation; 
    point.title = @"Where am I?"; 
    point.subtitle = @"I'm here!!!"; 


    [self.mapView addAnnotation:point ]; 

如何更改註釋的形象?

回答

0

試試這個,

MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
[[self.mapView viewForAnnotation:point] setTag:1]; 
UIImage *image = [UIImage imageNamed:@"pinIcon.png"]; 
[[self.mapView viewForAnnotation:point] setImage:image]; 
+0

不幸的是,不工作 – Noor

+0

如果它不工作,爲什麼它被標記爲正確的答案? – arniotaki

+0

無法正常工作.... –

1

創建一個自定義的註釋類。

CustomAnnotation.h

#import <MapKit/MapKit.h> 

@interface CustomAnnotation : MKAnnotationView 

@end 

CustomAnnotation.m

#import "CustomAnnotation.h" 

@implementation CustomAnnotation 

-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier { 

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 
    if (self != nil) { 
     CGRect frame = self.frame; 
     frame.size = CGSizeMake(46.0, 49.0); 
     self.frame = frame; 
     self.backgroundColor = [UIColor clearColor]; 
     self.centerOffset = CGPointMake(-5, -5); 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    [[UIImage imageNamed:@"IMAGE_NAME"] drawInRect:rect]; 

} 
我們MapView的委託方法

viewForAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 


    static NSString *defaultPinId = @"Pin"; 
    CustomAnnotation *pinView = (CustomAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinId]; 

    if (pinView == nil) { 

     pinView = [[CustomAnnotation alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinId]; 

    } 
    else { 
     pinView.annotation = annotation; 
    } 

    return pinView; 

} 
+0

這個作品,但我失去了標題和副標題 – Noor

+0

雖然,我宣佈標題和副標題,我沒有看到他們 – Noor