【问题标题】:Custom MKAnnotaionView, Display title/subtitle Position on Map自定义 MKAnnotationView,在地图上显示标题/副标题位置
【发布时间】:2023-03-19 22:55:01
【问题描述】:

1) 我的注释显示了not的标题和副标题。 我已经尝试了很多,但我不能。

2) 如果选择了注释,则注释将位于窗口中间。所以地图被移动了。有什么想法吗?

3) 我的 Callout 按钮不再起作用,我有 @ selector (openAnything) 但我想使用此功能被触发 - (void) mapView: (MKMapView *) mapView annotationView: (MKAnnotationView *) view calloutAccessoryControlTapped: (UIControl *) control {

问够了,这里有一个视频和一些源代码 http://www.youtube.com/watch?v=Ur1aqeYEFHw&feature=youtube_gdata_player

谢谢

TAPPMapViewControler.m

- (MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation {

if ([annotation isKindOfClass:[TAPPMapAnnotation class]])
{
    static NSString *shopAnnotationIdentifier = @"Filiale";

    //pinView = (MKPinAnnotationView *)
    TAPPMapAnnotationCustom* pinView = (TAPPMapAnnotationCustom*)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotationIdentifier];
    [self.myMapView dequeueReusableAnnotationViewWithIdentifier:shopAnnotationIdentifier];
    if (pinView == nil)
    {

        // if an existing pin view was not available, create one
        TAPPMapAnnotationCustom *customPinView = [[TAPPMapAnnotationCustom alloc] initWithAnnotation:annotation reuseIdentifier:shopAnnotationIdentifier];
        customPinView.image          = [UIImage imageNamed:@"map_pin.png"];


        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}
return nil;
}


-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
TAPPMapAnnotation *theAnnotation = view.annotation;
TAPPMapAnnotationDetail *theController = [self.storyboard instantiateViewControllerWithIdentifier:@"MapAnnotationDetail"];
theController.theAnnotationId   = theAnnotation.objectId;
[self.navigationController pushViewController:theController animated:YES];

}

TAPPMapAnnotationCustom.h

#import <MapKit/MapKit.h>

@interface TAPPMapAnnotationCustom : MKAnnotationView

@property (strong, nonatomic) UIImageView *calloutView;
@property (strong, nonatomic) UILabel *title;
@property (strong, nonatomic) UILabel *subTitle;


- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
- (void)animateCalloutAppearance:(BOOL)inAdd;

@end

TAPPMapAnnotationCustom.m

#import "TAPPMapAnnotationCustom.h"
#import "TAPPMapAnnotation.h"

@implementation TAPPMapAnnotationCustom


- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

    [super setSelected:selected animated:animated];

    if(selected)
    {
        // Remove Image, because we set a second large one.
        self.image = Nil;
        UIImage *imageBack = [[UIImage imageNamed:@"map_pin.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 35, 0, 6)];
        self.calloutView = [[UIImageView alloc]initWithImage:imageBack];
        [self.calloutView  setFrame:CGRectMake(0,0,0,0)];
        [self.calloutView  sizeToFit];
        [self addSubview:self.calloutView ];


        // Callout Button
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self action:@selector(openAnything) forControlEvents:UIControlEventTouchUpInside];

        rightButton.frame = CGRectMake(0
                                       , ((self.calloutView.frame.size.height-6) / 2)-(rightButton.frame.size.height / 2)
                                       , rightButton.frame.size.width
                                       , rightButton.frame.size.height);

        self.rightCalloutAccessoryView = rightButton;
        self.rightCalloutAccessoryView.hidden = YES;
        self.rightCalloutAccessoryView.alpha = 0;
        [self addSubview:self.rightCalloutAccessoryView];


        // Start Annimation 
        [self animateCalloutAppearance:YES];



    } else {
        //Start Annimation and Remove from view
        [self animateCalloutAppearance:NO];
    }
}


- (void)didAddSubview:(UIView *)subview{

    if ([[[subview class] description] isEqualToString:@"UICalloutView"]) {
        for (UIView *subsubView in subview.subviews) {
            if ([subsubView class] == [UIImageView class]) {
                UIImageView *imageView = ((UIImageView *)subsubView);
                [imageView removeFromSuperview];
            }else if ([subsubView class] == [UILabel class]) {
                UILabel *labelView = ((UILabel *)subsubView);
                [labelView removeFromSuperview];
            }
        }
    }
}


- (void)animateCalloutAppearance:(BOOL)inAdd {

    if (inAdd == YES) {

        self.rightCalloutAccessoryView.hidden = NO;

        [UIView animateWithDuration:0.4 delay:0 options: UIViewAnimationOptionTransitionNone
                         animations:^{

                             self.calloutView.frame = CGRectMake(self.calloutView.frame.origin.x
                                                                 , self.calloutView.frame.origin.y
                                                                 , self.calloutView.frame.size.width+150
                                                                 , self.calloutView.frame.size.height);

                             self.rightCalloutAccessoryView.alpha = 1;
                             self.rightCalloutAccessoryView.frame = CGRectMake(self.calloutView.frame.size.width - (self.rightCalloutAccessoryView.frame.size.width)
                                                                               , self.rightCalloutAccessoryView.frame.origin.y
                                                                               , self.rightCalloutAccessoryView.frame.size.width
                                                                               , self.rightCalloutAccessoryView.frame.size.height);


                         } completion:^(BOOL finished){ }];

    } else {


        [UIView animateWithDuration:0.4 delay:0 options: UIViewAnimationOptionTransitionNone
                         animations:^{

                             self.rightCalloutAccessoryView.alpha = 0;
                             self.rightCalloutAccessoryView.frame = CGRectMake(0
                                                                               , self.rightCalloutAccessoryView.frame.origin.y
                                                                               , self.rightCalloutAccessoryView.frame.size.width
                                                                               , self.rightCalloutAccessoryView.frame.size.height);


                             self.calloutView.frame = CGRectMake(self.calloutView.frame.origin.x
                                                                 , self.calloutView.frame.origin.y
                                                                 , self.calloutView.frame.size.width-150
                                                                 , self.calloutView.frame.size.height);




                         } completion:^(BOOL finished){

                             self.image = [UIImage imageNamed:@"map_pin.png"];
                             [self.calloutView  removeFromSuperview];
                             [self.rightCalloutAccessoryView removeFromSuperview];

                         }];
    }
}

@end

【问题讨论】:

    标签: position mkmapview mkannotationview callout


    【解决方案1】:

    你的注解类需要有subtitle属性,你的有subTitle

    如果calloutAccessoryControlTapped 没有被调用,这通常表明您的 MKMapview 的委托尚未设置。但是,您仅在选择附件按钮时才对其进行设置。你有充分的理由吗?由于标注窗口在被选中之前是不可见的,我建议您在viewForAnnotation 中设置附件按钮,并在标注窗口出现时让它出现。

    【讨论】:

    • 创建自定义标注是个大难题
    【解决方案2】:

    我修复了第一个:

        TAPPMapAnnotation *theAnnotation = (TAPPMapAnnotation *)self.annotation;
        self.title           = [[UILabel alloc]init];
        self.title.font      = [UIFont systemFontOfSize:12.0];
        self.title.textColor = [UIColor whiteColor];
        self.title.backgroundColor = [UIColor clearColor];
    
        self.title.text      = theAnnotation.title;
        [...]
        [self.theView addSubview:self.title];
    

    【讨论】:

      猜你喜欢
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2012-08-18
      相关资源
      最近更新 更多