मैं एक कस्टम MKAnnotationView के लिए फ्रेम स्थिति बदल सकता हूँ?

वोट
6

मैं उपवर्गीकरण द्वारा एक कस्टम एनोटेशन दृश्य बनाने के लिए कोशिश कर रहा हूँ MKAnnotationViewऔर अधिभावी drawRectविधि। मैं दृश्य एनोटेशन की स्थिति से ऑफसेट तैयार किया जा करना चाहते हैं, कुछ हद तक पसंद है MKPinAnnotationViewयह होता है, ताकि पिन के बिंदु, निर्दिष्ट निर्देशांक में है बल्कि पिन के बीच से। इसलिए मैं फ्रेम स्थिति और आकार के रूप में नीचे दिखाया गया है निर्धारित किया है। हालांकि, यह नहीं दिखता है जैसे मैं बिल्कुल ही आकार फ्रेम की स्थिति को प्रभावित कर सकते हैं। छवि समाप्त होता है एनोटेशन स्थिति से अधिक किया जा रहा तैयार केन्द्रित। कैसे प्राप्त करने के लिए मैं क्या चाहते हैं पर कोई सुझाव?

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];
}
19/03/2010 को 18:13
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


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 के रूप में अपनी छवि को सूट करने के लिए की जरूरत है, और वांछित केंद्र बिंदु, और कॉलआउट बिंदु को समायोजित करें।

31/05/2010 को 19:23
का स्रोत उपयोगकर्ता

वोट
15

उपवर्ग MKAnnotationView के बजाय, आप एक सामान्य MKAnnotationView का उपयोग करने और स्थापित करने की कोशिश की है imageऔर centerOffsetगुण?

19/03/2010 को 20:28
का स्रोत उपयोगकर्ता

वोट
2

आपका UIAnnotationViewहमेशा एक ही पैमाने पर तैयार की है, नक्शे के ज़ूम स्तर फर्क नहीं पड़ता। यही कारण है कि है centerOffsetज़ूम स्तर के साथ बाध्य नहीं है।

annView.centerOffsetतुम क्या जरूरत है। आप देखते हैं कि आपके पिन अच्छा स्थान पर नहीं है (उदाहरण के लिए, नीचे बीच में एक छोटे से ले जाने के लिए जब आप ज़ूम स्तर को बदलने), यह है क्योंकि आप सही सेट नहीं किया centerOffset

वैसे, आप छवि के नीचे बीच में समन्वय, एक्स अपने के समन्वय की बात सेट करना चाहते हैं centerOffsetडिफ़ॉल्ट रूप से छवि 0.0f होना चाहिए, annotationView केंद्र के रूप में। इसलिए कोशिश करें :

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

[से उत्तर MKAnnotation छवि कस्टम पिन छवि के साथ ऑफसेट

09/03/2013 को 17:46
का स्रोत उपयोगकर्ता

वोट
-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;
}
04/08/2011 को 08:14
का स्रोत उपयोगकर्ता

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