7

MKAnnotationViewメソッドをサブクラス化してオーバーライドすることにより、カスタム注釈ビューを作成しようとしていますdrawRectMKPinAnnotationViewピンのポイントがピンの中央ではなく、指定された座標になるように、ビューを注釈の位置からずらして描画したいと思います。そこで、以下のようにフレームの位置とサイズを設定しました。ただし、フレームの位置にはまったく影響を与えないように見えます。サイズのみです。アノテーション位置を中心に画像が描画されてしまいます。私が望むものを達成する方法に関するヒントはありますか?

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];
}
4

4 に答える 4

19

centerOffsetを使用してみましたが、何らかの理由でズームインしたときに画像が正しい位置にありましたが、マップをズームアウトすると、画像が本来あるべき位置から離れてしまいました。これに対する修正は、オフセットを使用して画像フレームを設定することでした。これが私が使用したコードです(コールアウトがない場合はコールアウトのものを省略できます)、ここからピンを使用しました)

#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を調整して、画像、目的の中心点、およびcalloutpointに合わせます。

于 2010-05-31T18:23:32.837 に答える
16

MKAnnotationView をサブクラス化するのではなく、ジェネリック MKAnnotationView を使用してプロパティimagecenterOffsetプロパティを設定してみましたか?

于 2010-03-19T19:28:27.547 に答える
3

マップは常に同じ縮尺で描画され、マップのズーム レベルは関係ありませUIAnnotationViewん。そのためcenterOffset、ズーム レベルに拘束されません。

annView.centerOffset必要なものです。ピンが適切な位置にない場合 (たとえば、ズーム レベルを変更すると下部中央が少し移動するなど)、正しい を設定していないことが原因ですcenterOffset

ちなみに、座標のポイントを画像の下部中央に設定する場合centerOffset、デフォルトで注釈ビューが画像を中央に配置するため、x 座標は 0.0f にする必要があります。だから試してみてください:

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

[ さんからの回答MKAnnotation のカスタム ピン画像による画像オフセット

于 2013-03-09T16:46:08.353 に答える
-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;
}
于 2011-08-04T07:14:19.127 に答える