2011-09-02 27 views

回答

6

對於iOS 3.2或更高版本,使用可能更好更簡單與地圖視圖,而不是嘗試子類化和手動攔截觸摸。

首先,手勢識別添加到地圖視圖:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
     initWithTarget:self action:@selector(tapGestureHandler:)]; 
tgr.delegate = self; //also add <UIGestureRecognizerDelegate> to @interface 
[mapView addGestureRecognizer:tgr]; 
[tgr release]; 

接下來,實施shouldRecognizeSimultaneouslyWithGestureRecognizer並返回YES所以你雙擊手勢識別器可以同時工作,作爲地圖的(否則輕叩引腳贏得」噸得到由地圖自動處理):

​​3210

最後,實現手勢處理機:

- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr 
{ 
    CGPoint touchPoint = [tgr locationInView:mapView]; 

    CLLocationCoordinate2D touchMapCoordinate 
     = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; 

    NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f", 
     touchMapCoordinate.latitude, touchMapCoordinate.longitude); 
} 
+0

感謝很多人它的工作不錯.... –

1

它是一種棘手的事情要做。

首先,你需要繼承的MKMapView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

方法,你可以找到觸摸的位置,然後用這個方法

- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view 

你可以找到lat和長..

0

在斯威夫特3:

func tapGestureHandler(_sender: UITapGestureRecognizer) { 
    let touchPoint = _sender.location(in: mapView) 
    let touchMapCoordinate = mapView.convert(touchPoint, toCoordinateFrom:mapView) 

    print("latitude: \(touchMapCoordinate.latitude), longitude: \(touchMapCoordinate,longitude)") 
} 
相關問題