【问题标题】:How to show a mapview callout on view did load如何在视图上显示地图视图标注确实加载
【发布时间】:2012-11-16 23:05:11
【问题描述】:

我试图在地图视图加载后立即显示注释标注。我确定我错过了一个简单的东西,但找不到它。

感谢您的帮助。

这是我的代码:

我的.h:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapDetailViewController : UIViewController <MKMapViewDelegate> {

    MKMapView *mapView;

}


// map
@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic) double latDouble;
@property (nonatomic) double lngDouble;

@property (nonatomic) NSString *vendorStr;

@property (nonatomic) NSNumber *costAmount;


@end

我的.m:

#import "MapDetailViewController.h"

@interface MapDetailViewController ()

@end

#define THESPAN 0.01f;

@implementation MapDetailViewController
@synthesize mapView;
@synthesize latDouble, lngDouble;
@synthesize vendorStr, costAmount;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];

   // mapView.showsUserLocation = YES;
    mapView.delegate = self;

    MKCoordinateRegion region;

    MKCoordinateSpan span;
    span.longitudeDelta = THESPAN;
    span.latitudeDelta = THESPAN;

    CLLocationCoordinate2D annotationCoord;
    annotationCoord.latitude = latDouble;
    annotationCoord.longitude = lngDouble;


    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
    annotationPoint.coordinate = annotationCoord;
    annotationPoint.title = vendorStr;

    //convert cost amount/ price to string value for the subtitle
    NSString *costAmountStr = [costAmount stringValue];


    annotationPoint.subtitle = costAmountStr;
    [mapView addAnnotation:annotationPoint];


    region.center = annotationCoord;
    region.span = span;

    [mapView setRegion:region animated:YES];


}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *newAnnotation=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];

    UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [disclosureButton addTarget:self action:@selector(mapCallOutPressed:) forControlEvents:UIControlEventTouchUpInside];

    newAnnotation.rightCalloutAccessoryView = disclosureButton;
    newAnnotation.animatesDrop = YES;    
    newAnnotation.canShowCallout = YES;
    newAnnotation.annotation = annotation;
    newAnnotation.draggable = YES;
    newAnnotation.enabled = YES;
    newAnnotation.exclusiveTouch = YES;
    newAnnotation.highlighted = YES;
    newAnnotation.multipleTouchEnabled = YES;
    newAnnotation.pinColor = MKPinAnnotationColorRed;
    newAnnotation.userInteractionEnabled = YES;


    //button on the right for popup for pins
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(mapCallOutPressed:)
          forControlEvents:UIControlEventTouchUpInside];
    newAnnotation.rightCalloutAccessoryView = rightButton;


    //Create and add the right button to the callout
    UIButton* rightCalloutButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    rightCalloutButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [newAnnotation setRightCalloutAccessoryView:rightButton];


    //zoom button on the left of popup for pins
    UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [leftButton setTitle:annotation.title forState:UIControlStateNormal];
    [leftButton addTarget:self
                   action:@selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside];
    newAnnotation.leftCalloutAccessoryView = leftButton;

    UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tag.png"]];
    newAnnotation.leftCalloutAccessoryView = profileIconView;


    return newAnnotation;
}


- (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views{
    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
            [mapView selectAnnotation:currentAnnotation animated:YES];

    }
}

// method for the annotation detail disclosure button
-(void)mapCallOutPressed:(id)sender {

    NSLog(@"mapcallout pressed");

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

    标签: objective-c ios mkmapview mapkit


    【解决方案1】:

    当从mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views 调用[MKMapView selectAnnotation:animated:] 方法时,您应该稍微延迟调度它

    - (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views{
        int64_t delayInSeconds = 0.1;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
                [mapView selectAnnotation:currentAnnotation animated:YES];
    
            }
        });
    }
    

    * 编辑

    在 6.0.1 设备、6.0 和 5.1 模拟器上试用。它确实有效,我怀疑您没有通过传递 nil 来正确设置注释标题。

    尝试将标题更改为固定值,如下所示:

    annotationPoint.title = @"Hello world";
    

    * EDIT2:

    你的属性声明应该看起来更像

    @property (nonatomic, copy) NSString *vendorStr;
    @property (nonatomic, strong) NSNumber *costAmount;
    

    【讨论】:

    • 谢谢,但这没有用。不确定它是否有帮助,但如果我注释掉这一行 // newAnnotation.draggable = YES;我无法触摸显示标注的图钉。
    • 我复制粘贴了你的代码,在模拟器上试了试,它工作正常。尝试增加延迟并检查 annotation.title 属性是否不为零。
    • 当您触摸带有标题和副标题的图钉时会显示注释。它不会在负载时弹出。另一个奇怪的事情是,如果我禁用 newAnnotation.draggable 那么你就不能触摸 pin。
    【解决方案2】:

    尝试在添加注释后立即将这行代码添加到viewWillAppear 方法中:

        [mapView selectAnnotation:annotationPoint animated:YES];
    

    【讨论】:

    • 太糟糕了,这正是我在我的项目中所做的,它对我有用。尝试将它从 viewWillAppear 移动到 viewDidAppear。我的代码是在视图完全显示完成后调用的
    【解决方案3】:

    我在视图出现后等待了 0.8 秒。

    [self performSelector:@selector(testMethod) withObject:nil afterDelay:.8];
    
    -(void) testMethod
    {
        [self.mapView selectAnnotation:annotation animated:NO];
    }
    

    animated:NO成功了

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多