2013-05-12 56 views
5

我MKPointAnnotation應該與此代碼進行定製:爲什麼我的MKPointAnnotation不是自定義的?

-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{ 
    Pin = [[MKPointAnnotation alloc] init]; 
    Pin.title = title; 
    [Pin setCoordinate:Location]; 
    [self mapView:mapView viewForAnnotation:Pin].annotation = Pin; 
    return Pin; 
} 



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

     MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; 


     if(!pinView){ 
      pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; 
      pinView.animatesDrop = YES; 
      pinView.canShowCallout = YES; 
      pinView.enabled = YES; 
      UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      [PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\ 
      pinView.rightCalloutAccessoryView = PicButton; 
     } 
     else{ 
      pinView.annotation = annotation; 
     } 
    return pinView; 
} 

然而,出於某種原因,該引腳仍處於違約,誰能幫助我在這裏? 謝謝

回答

10

你不應該自己打電話viewForAnnotation。您只應將註釋添加到您的地圖視圖中,然後確定哪些視圖在任何給定時刻都可見,併爲您撥打viewForAnnotation。因此:

  • 鑑於不同的註釋可以有不同的圖像,你真的想繼承MKPointAnnotation並給它imageName(注意,不是UIImage本身的屬性,而是一個名稱或標識符viewForAnnotation就能用來創建UIImage本身):

    @interface MyAnnotation : MKPointAnnotation 
    @property(nonatomic, strong) NSString *imageName; 
    @end 
    
    @implementation MyAnnotation 
    @end 
    
  • 添加註釋(不註釋視圖)添加到您MapView類,如:

    - (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName 
    { 
        MyAnnotation *annotation = [[MyAnnotation alloc] init]; 
        annotation.title = title; 
        annotation.coordinate = coordinate; 
        annotation.imageName = imageName; 
        [mapView addAnnotation:annotation]; 
        return annotation; 
    } 
    

    請注意,我不會建議將Pin作爲ivar /屬性,並且我會使用camelCase作爲變量名稱(以小寫開頭),我會將方法名稱從setAnnotation更改爲addAnnotationWithTitle等。

  • 確保您的控制器已被指定爲mapView的代表;

  • viewForAnnotation將被調用(如果註釋是可見的)。它可能應該是這樣的:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
    { 
        if ([annotation isKindOfClass:[MKUserLocation class]]) 
         return nil; 
    
        if ([annotation isKindOfClass:[MyAnnotation class]]) 
        { 
         MyAnnotation *customAnnotation = annotation; 
    
         static NSString *annotationIdentifier = @"MyAnnotation"; 
         MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier]; 
         if (!annotationView) 
         { 
          annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier]; 
          annotationView.canShowCallout = YES; 
          annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
         } 
         else 
         { 
          annotationView.annotation = annotation; 
         } 
    
         annotationView.image = [UIImage imageNamed:customAnnotation.imageName]; 
    
         return annotationView; 
        } 
    
        return nil; 
    } 
    

    注意,animatesDropMKPinAnnotationView選項,你不能自定義註解視圖做。但是,如果你想要它,很容易實現(見How can I create a custom "pin-drop" animation using MKAnnotationView?)。我建議你先解決基本的註釋視圖,然後再看看自定義註釋視圖放置動畫。

+0

但是,如何調整註釋的設置呢?因爲現在我使用像pinView.rightCalloutAccessoryView = PicButton;但是這也行不通 – 2013-05-12 12:38:02

+0

@BlackMagic查看我的修訂答案和更完整的代碼示例。 – Rob 2013-05-12 12:39:17

+1

非常感謝,這正是我所需要的。 – 2013-05-12 12:55:37

相關問題