2011-09-18 43 views
10

Tôi đã tạo chú thích mà tôi đang thêm vào MKMapView. Làm thế nào tôi sẽ đi về việc có một hình ảnh tùy chỉnh thay vì pin màu đỏ tiêu chuẩn?Hình ảnh tùy chỉnh cho MKAnnotation

@interface AddressAnnotation : NSObject<MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
    MKPinAnnotationColor pinColor; 
} 
@property (nonatomic,retain) NSString *title; 
@property (nonatomic,retain) NSString *subtitle; 
@property (nonatomic, assign) MKPinAnnotationColor pinColor; 
@end 

Trả lời

1

Ghi đè phương thức đại biểu mapView:viewForAnnotation:. Nếu annotation điểm param cho một trong các chú thích tùy chỉnh của bạn, hãy trả về chế độ xem tùy chỉnh theo ý thích của bạn.

+0

Bạn có thể chỉ cho tôi một ví dụ về cách thực hiện việc này không? Tôi mới đến mục tiêu c. Cảm ơn – 3sl

18

MKMapView được xem pin của nó từ phương pháp đại biểu của nó mapView:viewForAnnotation: Vì vậy, bạn phải:

  1. Đặt điều khiển xem của bạn khi đại biểu của bản đồ.
  2. Triển khai mapXem: viewForAnnotation: trong bộ điều khiển của bạn.

Đặt điều khiển của bạn như đại biểu

@interface MapViewController : UIViewController <MKMapViewDelegate> 

Đánh dấu giao diện với các giao thức đại biểu. Điều này cho phép bạn thiết lập bộ điều khiển như là người đại diện của MKMapView trong Interface Builder (IB). Mở tệp .xib chứa bản đồ của bạn, nhấp chuột phải vào MKMapView và kéo lối ra delegate vào bộ điều khiển của bạn.
Nếu bạn muốn sử dụng mã thay vì IB, hãy thêm self.yourMapView.delegate=self; vào phương thức viewDidLoad của bộ điều khiển. Kết quả sẽ giống nhau.

Thực hiện MapView: viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    // this part is boilerplate code used to create or reuse a pin annotation 
    static NSString *viewId = @"MKPinAnnotationView"; 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView*) 
     [self.mapView dequeueReusableAnnotationViewWithIdentifier:viewId]; 
    if (annotationView == nil) { 
     annotationView = [[[MKPinAnnotationView alloc] 
      initWithAnnotation:annotation reuseIdentifier:viewId] autorelease]; 
    } 
    // set your custom image 
    annotationView.image = [UIImage imageNamed:@"emoji-ghost.png"]; 
    return annotationView; 
} 
0

Để thiết lập hình ảnh tùy chỉnh thay vì standart MKPinAnnotationView cách duy nhất để làm điều đó là sử dụng MKAnnotationView với chức năng - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation. Dưới đây là ví dụ:

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

    if ([annotation isKindOfClass:[MKUserLocation class]]) { 
       return nil; 
    } 

    static NSString *identifier = @"Annotation"; 

    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if (!aView) { 
      aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
      aView.image = [UIImage imageNamed:@"Untitled1.png"]; 
      aView.canShowCallout = YES; 
      aView.draggable = YES; 
    } else { 
      aView.annotation = annotation; 
    } 

    return pin; 
} 

Giá trị aView.image Bạn có thể đặt hình ảnh của riêng bạn. Và cũng xem xét tham chiếu lớp MKAnnotationView để xử lý tốt hơn với nó.

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