31

我正在尝试捕获我的点击事件MKMapView,这样我可以MKPinAnnotation在用户点击的点上放置一个。基本上,我有一张地图覆盖MKOverlayViews(显示建筑物的覆盖),我想在用户点击该覆盖时通过拖放 aMKPinAnnotaion并在标注中显示更多信息来为用户提供有关该覆盖的更多信息。谢谢你。

4

4 回答 4

59

您可以使用 aUIGestureRecognizer来检测地图视图上的触摸。

但是,我建议不要单击一次,而是使用双击 ( 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];
}
于 2010-11-30T21:26:12.053 回答
5

我设置了一个长按(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]);
    }   
}
于 2012-03-23T04:10:14.863 回答
2

如果您想在地图视图中单击/点击,这是我正在使用的代码片段。(可可和斯威夫特)

let gr = NSClickGestureRecognizer(target: self, action: "createPoint:")
gr.numberOfClicksRequired = 1
gr.delaysPrimaryMouseButtonEvents = false // allows +/- button press
gr.delegate = self
map.addGestureRecognizer(gr)

在手势委托方法中,一个更喜欢双击手势的简单测试......</p>

func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool {
  let other = otherGestureRecognizer as? NSClickGestureRecognizer
  if (other?.numberOfClicksRequired > 1) {
    return true; // allows double click
  }

  return false
}

如果您希望地图处于各种“状态”,您还可以在其他委托方法中过滤手势,其中一种允许单击/单击

于 2016-01-06T04:17:03.660 回答
0

出于某种原因,UIGestureRecognizer 在 Swift 中对我不起作用。当我使用 UIGestureRecognizer 方式时。当我使用 touchesEnded 方法时,它返回一个 MKNewAnnotationContainerView。似乎这个 MKNewAnnotationContainerView 阻止了我的 MKMapView。幸运的是,它是 MKMapView 的子视图。所以我循环遍历 MKNewAnnotationContainerView 的 superviews 直到 self.view 来获取 MKMapView。我设法通过点击固定mapView。

斯威夫特 4.1

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    let t = touches.first
    print(t?.location(in: self.view) as Any)
    print(t?.view?.superview?.superview.self as Any)
    print(mapView.self as Any)
    var tempView = t?.view
    while tempView != self.view {
        if tempView != mapView {
            tempView = tempView?.superview!
        }else if tempView == mapView{
            break
        }

    }
  let convertedCoor = mapView.convert((t?.location(in: mapView))!, toCoordinateFrom: mapView)
   let pin =  MKPointAnnotation()
    pin.coordinate = convertedCoor
    mapView.addAnnotation(pin)
}
于 2018-08-26T13:00:01.500 回答