2010-03-19 38 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 2010-03-21 05:56:14

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上沒有問題) – 2013-11-05 15:04:08

+1

我對我的註釋視圖使用了完全不同的形狀,而這似乎無法解決問題。另外,我不認爲這個解決方案與根據縮放級別調整註釋的位置有關。 – 2014-01-20 08:52:13

+0

我也經歷過這種情況。我必須將CenterOffset設置爲0,0,否則它會將圖像與在偏移x/y比率的某個函數中移動的地圖一起轉換。不知道爲什麼。 – 2015-05-31 01:21:05

-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座標應該是0.0f,因爲默認情況下,annotationView居中圖像。因此,嘗試:從[MKAnnotation image offset with custom pin image

+3

謝謝你的工作 – 2013-09-24 13:31:47

+0

很高興聽到它;) – VICTORGS3 2013-10-24 11:46:29

+5

好的copypasta從這個答案:http://stackoverflow.com/a/8165645/674009 – mattyohe 2014-02-24 19:21:15