【问题标题】:How to add a push pin to a MKMapView(IOS) when touching?触摸时如何向 MKMapView(IOS) 添加图钉?
【发布时间】:2011-04-26 23:26:17
【问题描述】:

我必须获取用户在 MKMapView 上触摸的点的坐标。 我没有使用 Interface Builder。 可以举个例子吗?

【问题讨论】:

    标签: iphone ios mkmapview pushpin


    【解决方案1】:

    您可以为此使用UILongPressGestureRecognizer。无论您在哪里创建或初始化地图视图,首先将识别器附加到它:

    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];
    

    然后在手势处理程序中:

    - (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 是您定义的符合MKAnnotation 协议的类。如果您的应用仅在 iOS 4.0 或更高版本上运行,您可以改用预定义的 MKPointAnnotation 类。

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

    【讨论】:

    • 很棒的答案,谢谢。就我个人而言,我将 if 语句翻转为 ==,因此如果它不是 UIGestureRecognizerStateBegan,它就会返回。这样做会在指定时间后放下图钉,即使用户仍然拿着我想要的地图(以及官方地图应用程序是如何做到的)。
    • 我只想说我在我的项目中实现了你的答案,它就像一个魅力。感谢您最出色的回答。
    • 这很完美,但只在我的模拟器中。物理电话上没有回拨。有任何想法吗?我正在使用 ARC 运行 iOS5。
    • @rjgonzo:应该可以在 iOS5、ARC 和设备上正常工作。尝试从设备中删除应用程序并执行清理、重建和重新安装。在设备上运行时,添加断点或 NSLogs 以确保添加 lpgr 时 mapView 不为零。
    • 可以做这个动画吗?
    【解决方案2】:

    感谢 Anna 提供了这么好的答案!如果有人感兴趣,这里是一个 Swift 版本(答案已更新为 Swift 4.1 语法)。

    创建 UILongPressGestureRecognizer:

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

    处理手势:

    @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)
    }
    

    【讨论】:

    • 哇...我没有注意到这一点,花了很长时间转换它。
    • 可以使用 let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
    • @Dx_ 可以,因为识别器没有被修改。正在修改识别器中的属性。
    • 我在 Swift 3 中收到错误消息。错误是:“使用未解析的标识符 'gestureRecogniser' 有人有解决方案吗?
    • 嗨@PhilipS,我已经更新了对 Swift 3.0 语法的回答。我希望它会有所帮助。
    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多