Как записать жест касания в MKMapView - PullRequest
31 голосов
/ 30 ноября 2010

Я пытаюсь зафиксировать событие касания на моем MKMapView, таким образом я могу сбросить MKPinAnnotation на точку, где пользователь нажал. По сути, у меня есть карта, наложенная на MKOverlayViews (наложение, показывающее здание), и я хотел бы дать пользователю больше информации об этом наложении, когда он нажимает на него, опуская MKPinAnnotaion и показывая дополнительную информацию в выноске. Спасибо.

Ответы [ 4 ]

59 голосов
/ 01 декабря 2010

Вы можете использовать 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];
}
5 голосов
/ 23 марта 2012

Я установил долгое нажатие (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]);
    }   
}
2 голосов
/ 06 января 2016

Если вы хотите использовать один щелчок / нажатие в виде карты, вот фрагмент кода, который я использую. (Какао и Свифт)

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

в методе делегата жестов, простой тест для предпочтения жеста двойного касания ...

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

  return false
}

Вы также можете отфильтровать жест в других методах делегата, если хотите, чтобы Карта находилась в различных «состояниях», одно из которых позволяло одно нажатие / нажатие

0 голосов
/ 26 августа 2018

По какой-то причине UIGestureRecognizer просто не работал для меня в Swift.Когда я использую способ UIGestureRecognizer.Когда я использовал метод touchesEnded, он возвращает MKNewAnnotationContainerView.Кажется, что этот MKNewAnnotationContainerView заблокировал мой MKMapView.К счастью, это подпредставление MKMapView.Поэтому я просмотрел суперпредставления MKNewAnnotationContainerView до self.view, чтобы получить MKMapView.И мне удалось закрепить mapView, нажав.

Swift 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)
}
...