iOS7.1 MKMapView selectAnnotation问题

时间:2014-04-11 14:15:50

标签: objective-c mkmapview mkannotationview ios7.1

所以自从iOS7.1问世以来,当用户点击地图时,我的代码一直存在问题,无法在地图上选择新的注释。该代码可以在iOS6和7中找到,但是在7.1上会搞乱。

我没有找到关于这个问题的任何具体信息,但这是我用来说明问题的非常简单的代码。

我的标题只包含此内容:

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

@interface MAViewController : UIViewController <MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *mMapView;

@end

我的实现文件如下:

#import "MAViewController.h"

@interface MAViewController ()

@end

@implementation MAViewController

@synthesize mMapView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.mMapView setDelegate:self];
    [self.mMapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapped:)]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)mapTapped:(UITapGestureRecognizer *)recognizer
{

    CGPoint touchPoint = [recognizer locationInView:self.mMapView];
    CLLocationCoordinate2D touchMapCoordinate = [self.mMapView convertPoint:touchPoint toCoordinateFromView:self.mMapView];
    CLLocation *lTapLocation = [[CLLocation alloc] initWithLatitude:touchMapCoordinate.latitude longitude:touchMapCoordinate.longitude];

    [self.mMapView removeAnnotations:mMapView.annotations];

    MKPointAnnotation *lAnnotation = [[MKPointAnnotation alloc] init];
    lAnnotation.coordinate = lTapLocation.coordinate;
    lAnnotation.title = @"TAP";

    [self.mMapView addAnnotation:lAnnotation];
}

#pragma mark - MapView Delegate

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

    if([annotation isKindOfClass: [MKUserLocation class]]) {
        return nil;
    }

    MKPinAnnotationView *lPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    lPin.canShowCallout = FALSE;

    return lPin;
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *lAnnotationView = (MKAnnotationView*)[views objectAtIndex:0];
    lAnnotationView.canShowCallout = YES;

    if ([lAnnotationView.annotation isKindOfClass:[MKUserLocation class]]) {
        return;
    }

    [mapView selectAnnotation:lAnnotationView.annotation animated:YES];
}

@end

我刚刚在故事板中添加了一个MKMapView,仅此而已。 感谢您分享您对此问题的看法。

问题是,一次选择并取消选择注释,用户需要再次点击它以显示标题。

1 个答案:

答案 0 :(得分:2)

我知道这个问题是在两个月前被问到的,但我目前面临同样的问题。

目前,我正在使用一种解决方法,在将注释添加到mapView后,我会稍微选择注释。这似乎与延迟&gt; = 0.4s一起使用。但随着延迟&lt; 0.4s在选择后立即取消选择。很奇怪:

//In the onClick handler
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = tapCoord;
self.lastAnnotation = annotation;
[self.mapView addAnnotation:annotation];
[self performSelector:@selector(selectLastAnnotation) withObject:nil afterDelay:0.4];

和辅助方法:

-(void)selectLastAnnotation{
    [self.mapView selectAnnotation:self.lastAnnotation animated:YES];
}