sbk*_*bkb 31 mkpinannotationview mkmapview ios4 mkoverlay uitapgesturerecognizer
我正在尝试捕捉我的点击事件MKMapView,这样我就可以MKPinAnnotation在用户点击的点上删除.基本上我有一个覆盖的地图MKOverlayViews(显示建筑物的叠加层),我想通过删除MKPinAnnotaion并在标注中显示更多信息来为用户提供有关该叠加的更多信息.谢谢.
小智 59
您可以使用a UIGestureRecognizer来检测地图视图上的触摸.
然而,我建议寻找双击(UITapGestureRecognizer)或长按(UILongPressGestureRecognizer),而不是单击.单击可能会干扰用户尝试单击引脚或标注本身.
在您设置地图视图的位置(viewDidLoad例如),将手势识别器附加到地图视图:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];
或使用长按:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleGesture:)];
lpgr.minimumPressDuration = 2.0;  //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
在handleGesture:方法中:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
        return;
    CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
    CLLocationCoordinate2D touchMapCoordinate = 
        [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
    MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
    pa.coordinate = touchMapCoordinate;
    pa.title = @"Hello";
    [mapView addAnnotation:pa];
    [pa release];
}
我设置了长按(UILongPressGestureRecognizer),viewDidLoad:但它只检测到第一次触摸的唯一一次.
我在哪里可以设置长按以检测所有触摸?(这意味着每次等待用户触摸屏幕推针时都准备好了地图)
该viewDidLoad:方法!
- (void)viewDidLoad {
    [super viewDidLoad];mapView.mapType = MKMapTypeStandard;
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    [self.mapView addGestureRecognizer:longPressGesture];
    [longPressGesture release];
    mapAnnotations = [[NSMutableArray alloc] init];
    MyLocation *location = [[MyLocation alloc] init];
    [mapAnnotations addObject:location];
    [self gotoLocation];
    [self.mapView addAnnotations:self.mapAnnotations];
}
和handleLongPressGesture方法:
-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
    // This is important if you only want to receive one tap and hold event
    if (sender.state == UIGestureRecognizerStateEnded)
    {NSLog(@"Released!");
        [self.mapView removeGestureRecognizer:sender];
    }
    else
    {
        // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
        CGPoint point = [sender locationInView:self.mapView];
        CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        // Then all you have to do is create the annotation and add it to the map
        MyLocation *dropPin = [[MyLocation alloc] init];
        dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
        dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
//        [self.mapView addAnnotation:dropPin];
        [mapAnnotations addObject:dropPin];
        [dropPin release];
        NSLog(@"Hold!!");
        NSLog(@"Count: %d", [mapAnnotations count]);
    }   
}
| 归档时间: | 
 | 
| 查看次数: | 25364 次 | 
| 最近记录: |