Nasıl bir özel MKAnnotationView için çerçeve konumunu nasıl değiştirebilirim?

oy
6

Ben sınıflara göre özel açıklama görünümü yapmaya çalışıyorum MKAnnotationViewve geçersiz kılma drawRectyöntemi. Ben biraz gibi, görünüm ek açıklamanın konumundan ofset çizilir verilsin MKPinAnnotationViewpimin noktası yerine pimin orta daha belirtilen koordinatlarda olduğu şekilde, yapar. Aşağıda gösterildiği gibi Yani çerçeve konumunu ve boyutunu ayarlayın. Ben, hiç yalnız boyutu çerçevesinin konumunu etkileyebilir gibi Ancak, görünmüyor. Resim çekilmiş açıklama konumu üzerinde merkezlenmiş olan biter. İstediğimi elde etmek konusunda herhangi bir ipucu?

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];
}
Oluştur 19/03/2010 saat 18:13
kaynak kullanıcı
Diğer dillerde...                            


4 cevaplar

oy
19

Ben centerOffset ve yakınlaştırıldığında görüntü sağ konumdaydı nedense kullanarak çalıştı ama sen uzakta gerekiyordu yerden hareket edeceğini resmin dışarı harita uzaklaştırdınız gibi olması. Buna düzeltme offset ile görüntü çerçevesinin ayarlanması oldu. İşte kullanılan kod (bir ek bilgiyi yoksa sen çizgisi atlayabiliriz), ben den işaretçilerine kullanılan burada )

#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 ve resme uygun şekilde gerektiği gibi calloutOffset ve istenen merkez noktasını ve çizgisi noktasını ayarlayın.

Cevap 31/05/2010 saat 19:23
kaynak kullanıcı

oy
15

Aksine alt sınıf MKAnnotationView yerine, genel bir MKAnnotationView kullanarak ve ayar denediniz imageve centerOffsetözelliklerini?

Cevap 19/03/2010 saat 20:28
kaynak kullanıcı

oy
2

Sizin UIAnnotationViewher zaman aynı ölçekte çizilir, haritanın zum seviyesi önemli değildir. Bu yüzden centerOffsetyakınlaştırma düzeyi ile bağlı değildir.

annView.centerOffsetne ihtiyaç vardır. Eğer pim (zoom seviyesini değiştirirken örneğin, alt merkezi biraz hareket) iyi konumda olmadığını görürseniz hakkını kurmadı, bunun nedenicenterOffset .

Bu arada, görüntünün alt ortasında x senin koordinatı koordinat noktasını ayarlamak isterseniz centerOffsetvarsayılan olarak annotationView merkezi olarak, 0.0f olmalıdır görüntüyü. O zaman dene :

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

[Dan Cevap özel dağılım görüntü ile ofset MKAnnotation görüntüsü

Cevap 09/03/2013 saat 17:46
kaynak kullanıcı

oy
-3

Örneğin BadPirate için teşekkürler.

Ben UIImageView kullanarak çalıştı, ancak bunun gerekli olduğunu sanmıyorum.

Benim sınıf bu benziyordu

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

    NSString *stitle;
}

ve uygulama

- (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;
}
Cevap 04/08/2011 saat 08:14
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more