代码之家  ›  专栏  ›  技术社区  ›  andrei

如何更改自定义mkannotationView的框架位置?

  •  6
  • andrei  · 技术社区  · 15 年前

    我正试图通过子类化来创建自定义注释视图 MKAnnotationView 并超越 drawRect 方法。我希望视图从注释位置偏移,有点像 MKPinAnnotationView 这样做是为了使销的点位于指定的坐标,而不是销的中间。所以我设置了框架的位置和大小,如下所示。但是,看起来我根本不能影响框架的位置,只影响大小。图像最终以注释位置为中心绘制。关于如何实现我想要的有什么建议吗?

    myAnnotationView.h:我的注释视图。

    @interface MyAnnotationView : MKAnnotationView {
    
    }
    

    myannotationview.m:我的注释视图.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 回复  |  直到 10 年前
        1
  •  15
  •   Tom    15 年前

    您是否尝试使用通用的mkannotationView并设置 image centerOffset 属性?

        2
  •  19
  •   Community CDub    8 年前

    我尝试使用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以适合您的图像、所需的中心点和Callout点。

        3
  •  2
  •   Community CDub    8 年前

    您的 uiannotationView 始终以相同的比例绘制,地图的缩放级别无关紧要。这就是为什么 centeroffset is not bound with the zoom level.

    annview.centeroffset is what you need.如果您看到您的销不在正确的位置(例如,更改缩放级别时,底部中心移动一点),则是因为您没有设置正确的 centeroffset

    顺便说一下,如果要在图像底部中心设置坐标点,则 centeroffset 的x坐标应为0.0f,因为annotationView默认情况下会使图像居中。所以尝试:

    annview.centeroffset=cgpointmake(0,-imageheight/2);
    < /代码> 
    
    

    回答来自[mkannotation image offset with custom pin image

    EL。

    annView.centerOffset是你需要的。如果你看到你的别针不在正确的位置(例如,当你改变缩放级别时,底部的中心移动了一点),那是因为你没有设置正确的位置。中心偏移.

    顺便说一句,如果您想在图像的底部中心设置坐标点,您的中心偏移应为0.0f,因为默认情况下AnnotationView使图像居中。所以尝试:

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

    从[答案]mkannotation图像偏移与自定义销图像

        4
  •  -3
  •   NBALA    13 年前

    谢谢你的例子,坏蛋。

    我试过使用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;
    }