2010-03-19 4 views
6

MKAnnotationView을 서브 클래 싱하고 drawRect 메서드를 오버라이드하여 사용자 정의 주석보기를 만들려고합니다. 주석 위치에서 오프셋을 그려야합니다. 다소 MKPinAnnotationView과 같으므로 핀의 포인트는 핀의 중간이 아니라 지정된 좌표에 있습니다. 그래서 아래와 같이 프레임 위치와 크기를 설정합니다. 그러나 프레임의 위치에 전혀 영향을 줄 수없는 것처럼 보입니다. 크기 만 있습니다. 이미지가 주석 위치의 중앙에 그려집니다. 내가 원하는 것을 얻는 방법에 대한 조언?커스텀 MKAnnotationView의 프레임 위치는 어떻게 변경합니까?

MyAnnotationView.h :

@interface MyAnnotationView : MKAnnotationView { 

} 

MyAnnotationView.m :

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) { 
     self.canShowCallout = YES; 
     self.backgroundColor = [UIColor clearColor]; 
     // Position the frame so that the bottom of it touches the annotation position 
     self.frame = CGRectMake(0, -16, 32, 32); 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 
    [[UIImage imageNamed:@"blue-dot.png"] drawInRect:rect]; 
} 

답변

15

보다는 서브 클래스 MKAnnotationView, 당신은 일반적인 MKAnnotationView을 사용하고 imagecenterOffset 속성을 설정하려고 있나요?

+0

예에서

annView.centerOffset = CGPointMake(0, -imageHeight/2); 

답변. 그러나 나는 여전히 일반적인 경우에 대해 궁금해. – andrei

19

centerOffset을 사용해 보았습니다. 어떤 이유로 확대 된 이미지가 올바른 위치에 있었지만지도를 확대하면 이미지가 원래 위치에서 벗어났습니다. 이 문제를 해결하려면 오프셋을 사용하여 이미지 프레임을 설정해야합니다. 저는 여기에 사용되는 코드는 (당신은 선이없는 경우는 콜 아웃 물건을 남길 수 있습니다)입니다, 당신의 이미지에 맞게 필요에 따라 here에서 핀)

#pragma mark MKMapViewDelegate 
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if(![annotation isKindOfClass:[MyAnnotation class]]) // Don't mess user location 
     return nil; 

    MKAnnotationView *annotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:@"spot"]; 
    if(!annotationView) 
    { 
     annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot"]; 
     annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [(UIButton *)annotationView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside]; 
     annotationView.enabled = YES; 
     annotationView.canShowCallout = YES; 
     annotationView.centerOffset = CGPointMake(7,-15); 
     annotationView.calloutOffset = CGPointMake(-8,0); 
    } 

    // Setup annotation view 
    annotationView.image = [UIImage imageNamed:@"pinYellow.png"]; // Or whatever 

    return annotationView; 
} 

이 centerOffset 및 calloutOffset을 조정하여 사용하고, 원하는 중심점 및 콜 아웃 포인트.

+1

"이 문제를 해결하려면 오프셋을 사용하여 이미지 프레임을 설정했습니다."라고 제공된 코드는 문제를 해결하지 못합니다. 당신은 정교 할 수 있습니까?centerOffset이 iOS7에서 아무 작업도하지 않는 것 같습니다 (iOS6에서는 괜찮습니다) –

+1

주석보기에 완전히 다른 모양을 사용하고 있으며 문제를 해결하지 못하는 것 같습니다. 또한이 솔루션은 확대/축소 수준에 따라 주석 위치를 조정하는 것과 관련이 없다고 생각합니다. –

+0

나는 이것을 경험했다. CenterOffset을 0,0으로 설정해야합니다. 그렇지 않으면 오프셋 x/y 비율의 일부 함수에서 이동하는지도와 함께 이미지를 변환했습니다. 이유를 모르겠다. –

-3

예제 BadPirate에 감사드립니다.

나는 UIImageview를 사용해 보았지만, 필요 없다고 생각합니다.

내 클래스는이

@interface ImageAnnotationView : MKAnnotationView { 
    UIImageView *_imageView; 
    id m_parent; 
    BusinessMapAnnotation *m_annotation; 

    NSString *stitle; 
} 

그리고

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 
    self.frame = CGRectMake(0, 0, kWidth, kHeight); 
    self.backgroundColor = [UIColor clearColor]; 

    self.m_annotation = (BusinessMapAnnotation*)annotation; 

    NSString* categoryTitle = m_annotation.sCTitle; 

    NSString* l_titleString = @"NearPin.png"; 

    if (categoryTitle!= nil) { 
     if ([categoryTitle length] > 0) { 
      //NSLog(@"%@", categoryTitle); 
      l_titleString = [NSString stringWithFormat:@"Map%@.png", categoryTitle]; 
      //NSLog(@"%@", l_titleString); 
     } 
    } 

    self.stitle = m_annotation.sTitle; 

    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:([stitle isEqualToString:@"You are here"]) ? @"Pushpin_large.png":l_titleString]]; 
    _imageView.contentMode = UIViewContentModeCenter; 

    _imageView.frame = CGRectMake(kBorder, kBorder, kWidth, kHeight); 

    [self addSubview:_imageView]; 
    _imageView.center = ([stitle isEqualToString:@"You are here"]) ? CGPointMake(15.0, -10.0):CGPointMake(kWidth/2, 0.0); 

    return self; 
} 
2

당신의 UIAnnotationView 항상 같은 규모로 그려진지도의 줌 레벨은 중요하지 않습니다 구현처럼 보였다. 그렇기 때문에 centerOffset은 확대/축소 레벨과 연결되지 않습니다.

annView.centerOffset입니다. 핀이 좋은 위치에 있지 않은 경우 (예 : 줌 레벨을 변경할 때 하단 중앙이 약간 움직임), 오른쪽을 설정하지 않았기 때문입니다. centerOffset.

그런데 이미지 하단 중앙에 좌표 점을 설정하려면 centerOffset의 x 좌표는 annotationView가 기본적으로 이미지 중심으로 0.0f 여야합니다. 그래서 시도 : (내 목적을 위해 충분한 사실이다) 이미지 기반 주석보기 작동 [MKAnnotation image offset with custom pin image

+3

고마워요. –

+0

듣고있어 기쁘게 생각합니다.) – VICTORGS3

+5

좋은 답변 : http://stackoverflow.com/a/8165645/674009 – mattyohe

관련 문제