2013-05-12 2 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 
    
  • 는 예를 들면, 당신의지도보기에 주석 (안 주석보기) 추가

    ,592,761,

    참고로, 저는 divacer/property를 Pin으로 만들고 싶지 않습니다. 변수 이름에 소문자로 시작하는 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; 
    } 
    

    주의 animatesDrop는 사용자 정의 주석 전망 할 수없는 MKPinAnnotationView 옵션입니다. 그러나 원하는 경우 구현하는 것이 쉽습니다 (How can I create a custom "pin-drop" animation using MKAnnotationView? 참조). 먼저 기본 주석 뷰를 처리하고 나중에 사용자 정의 주석 뷰 드롭 애니메이션을 살펴볼 것을 제안합니다.

+0

하지만 어떻게 주석의 설정을 조정합니까? 지금은 다음과 같은 것들을 사용하기 때문에 : pinView.rightCalloutAccessoryView = PicButton; 그러나 그것도 작동하지 않습니다 –

+0

@BlackMagic 더 완전한 코드 샘플을 가지고 수정 된 답변을보십시오. – Rob

+1

정말 고마워, 그게 내가 필요한거야. –

관련 문제