31

でタップ イベントをキャプチャしようとしています。このようにして、ユーザーがタップしたポイントにMKMapViewをドロップできます。基本的に、 (建物を示すオーバーレイ) でオーバーレイMKPinAnnotationされたマップがあり、ユーザーがそのオーバーレイをタップすると、吹き出しにドロップして詳細情報を表示することで、そのオーバーレイに関する詳細情報を提供したいと考えています。ありがとうございました。MKOverlayViewsMKPinAnnotaion

4

4 に答える 4

59

を使用し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];
}
于 2010-11-30T21:26:12.053 に答える
5

長押し ( UILongPressGestureRecognizer)を設定しましviewDidLoad:たが、最初から 1 回のタッチのみを検出します。

すべてのタッチを検出するために長押しをどこで設定できますか? (これは、ユーザーが画面にタッチしてピンを押すのを待つたびに、マップの準備ができていることを意味します)

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 に答える
0

何らかの理由で、UIGestureRecognizer が Swift で機能しませんでした。UIGestureRecognizer の方法を使用する場合。touchesEnded メソッドを使用すると、MKNewAnnotationContainerView が返されます。この MKNewAnnotationContainerView が私の MKMapView をブロックしたようです。幸いなことに、これは MKMapView のサブビューです。そこで、MKNewAnnotationContainerView のスーパービューを 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 に答える