2010-11-30 12 views
30

我正在嘗試在我的MKMapView上捕獲點按活動,這樣我可以在用戶點擊的位置放置MKPinAnnotation。基本上我有一張覆蓋MKOverlayViews(覆蓋顯示建築物)的地圖,我想通過刪除MKPinAnnotaion並在標註中顯示更多信息,爲用戶提供關於該覆蓋圖的更多信息。 謝謝。如何在MKMapView上捕獲點按手勢

回答

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]; 
} 
+0

謝謝你的建議,一旦我有它的工作,我會盡快回復。我確實嘗試過單擊,但之後我無法顯示我的PinAnnotations的標註。看起來我需要使用LongPressureGesture – sbkb 2010-12-02 16:09:46

5

我在viewDidLoad:中設置了一個長按(UILongPressGestureRecognizer),但它只檢測到第一個觸摸。

我在哪裏可以設置長按來檢測所有觸摸?(這意味着地圖準備就緒,每次等待用戶觸摸到屏幕推一個針)

該方法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]); 
    } 
} 
1

如果你想在地圖視圖使用一個單一的點擊/點按,這裏的代碼片段我使用。 (可可和斯威夫特)

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 
} 

你還可以過濾手勢在其他委託方法,如果你想要的地圖在各種「州」,其中一個允許單擊/點擊