2012-03-09 35 views
5

Tôi muốn thêm hình ảnh tùy chỉnh vào chú thích của mình trên bản đồ. Và tôi đã thực hiện MapAnnotationView tùy chỉnh sau:IOS: Thêm hình ảnh vào tùy chỉnh MKAnnotationview

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 
@class POI; 

@interface MapAnnotation : MKAnnotationView <MKAnnotation > 

@property (nonatomic) CGFloat lat; 
@property (nonatomic) CGFloat lon; 
@property (nonatomic) CGFloat altitude; 
@property (nonatomic, copy) NSString * title; 
@property (nonatomic, copy) NSString * subtitle; 
@property (nonatomic,retain) NSString *source; 
@property (nonatomic,retain) UIImage *image; 

@end 

@implementation MapAnnotation 
@synthesize coordinate; 
@synthesize lat=_lat,lon=_lon,altitude= _altitude; 
@synthesize subtitle= _subtitle, title= _title, source=_source, image =_img; 


- (CLLocationCoordinate2D)coordinate;{ 
    CLLocationCoordinate2D position; 
    if (_lat != 0.0 && _lon != 0.0) { 
     position.latitude = _lat; 
     position.longitude = _lon; 

    }else { 
     position.latitude=0.0; 
     position.longitude=0.0; 
    } 

    return position; 
} 

@end 

-(void) mapDataToMapAnnotations{ 

    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10]; 
    for (id annotation in _map.annotations) 
     if (annotation != _map.userLocation) 
      [toRemove addObject:annotation]; 
    [_map removeAnnotations:toRemove]; 

    [_data removeAllObjects]; 

    [_data addObjectsFromArray:[UDdelegate naturArray]]; 


    if(_data != nil){ 
     MapAnnotation * tmpPlace; 
     //for(NSDictionary * poi in _data){ 


     for(POI* poi in _data){ 

      tmpPlace = [[MapAnnotation alloc]init]; 

      tmpPlace.title = [poi title]; 
      tmpPlace.lat = [poi lat]; 
      tmpPlace.lon = [poi lon]; 
      tmpPlace.subtitle = [poi dist]; 
      tmpPlace.image = [poi poiIcon]; 

      [self.map addAnnotation:tmpPlace]; 
      [_map setNeedsLayout]; 
     } 
    } 
} 

Vấn đề là ghim là chuẩn redPin .... Tôi chắc chắn rằng các biểu tượng không phải là rỗng, đã kiểm tra điều đó.

Cảm ơn

Trả lời

11

Bạn có để phục vụ MapKit phương pháp đại biểu mapView:viewForAnnotation: với một giao diện tùy chỉnh.

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier"; 

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier]; 

    if (annotationView == nil) 
    { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier] autorelease]; 
    } 

    annotationView.image = [UIImage imageNamed:@"pin_image.png"]; 
    annotationView.annotation = annotation; 

    return annotationView; 
} 

Để đóng gói thêm, bạn nên tạo chế độ xem chú thích tùy chỉnh như bạn đã làm và phân phối phương thức đại biểu ở trên với lớp học của bạn.

Tôi khuyên bạn nên đổi tên lớp học MapAnnotation vì điều này gây nhầm lẫn. Ngoài ra còn có chú thích trong iOS là các chủ sở hữu dữ liệu cho các chế độ xem chú thích đó. Để giải quyết điều này tôi muốn viết kiểu của lớp thừa kế, trong trường hợp này là MKAnnotationView ở cuối lớp tùy chỉnh của bạn. Ví dụ: CustomPinAnnotationView.

+0

Trong trường hợp của tôi, phương thức ủy nhiệm trên không được gọi, ngay cả khi tôi đã thêm đại biểu . (và cũng được gán nó như là mapView.delegate = self;) – stack

Các vấn đề liên quan