触摸时如何向 MKMapView(IOS) 添加图钉?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3959994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:56:21  来源:igfitidea点击:

How to add a push pin to a MKMapView(IOS) when touching?

iphoneiosmkmapviewpushpin

提问by Christophe Debove

I had to get the coordinate of a point where the user touch on a MKMapView. I'm not working with the Interface Builder. Can you give me one example?

我必须获得用户在 MKMapView 上触摸的点的坐标。我不使用 Interface Builder。你能给我举个例子吗?

回答by

You can use a UILongPressGestureRecognizerfor this. Wherever you create or initialize the mapview, first attach the recognizer to it:

您可以为此使用UILongPressGestureRecognizer。无论您在何处创建或初始化 mapview,首先将识别器附加到它:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //user needs to press for 2 seconds
[self.mapView addGestureRecognizer:lpgr];
[lpgr release];

Then in the gesture handler:

然后在手势处理程序中:

- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
        return;

    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];   
    CLLocationCoordinate2D touchMapCoordinate = 
        [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

    YourMKAnnotationClass *annot = [[YourMKAnnotationClass alloc] init];
    annot.coordinate = touchMapCoordinate;
    [self.mapView addAnnotation:annot];
    [annot release];
}

YourMKAnnotationClass is a class you define that conforms to the MKAnnotationprotocol. If your app will only be running on iOS 4.0 or later, you can use the pre-defined MKPointAnnotationclass instead.

YourMKAnnotationClass 是您定义的符合MKAnnotation协议的类。如果您的应用仅在 iOS 4.0 或更高版本上运行,您可以改用预定义的MKPointAnnotation类。

For examples on creating your own MKAnnotation class, see the sample app MapCallouts.

有关创建您自己的 MKAnnotation 类的示例,请参阅示例应用MapCallouts

回答by Vlad Spreys

Thanks to Anna for providing such a great answer! Here is a Swift version if anybody is interested (the answer has been updated to Swift 4.1 syntax).

感谢安娜提供了如此出色的答案!如果有人感兴趣,这里有一个 Swift 版本(答案已更新为 Swift 4.1 语法)。

Creating UILongPressGestureRecognizer:

创建 UILongPressGestureRecognizer:

let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(MapViewController.handleLongPress(_:)))
longPressRecogniser.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPressRecogniser)

Handling the gesture:

处理手势:

@objc func handleLongPress(_ gestureRecognizer : UIGestureRecognizer){
    if gestureRecognizer.state != .began { return }

    let touchPoint = gestureRecognizer.location(in: mapView)
    let touchMapCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)

    let album = Album(coordinate: touchMapCoordinate, context: sharedContext)

    mapView.addAnnotation(album)
}