在Swift中,通过长按在地图视图上添加一个标注锚点

12
我正在尝试制作一个iPhone应用程序,需要用户能够长按地图视图上的某个位置以在那里放置标记。有人知道如何做到这一点吗?
当您在屏幕上长按时,这种行为可以在Apple地图中观察到。它会落下一个标记并显示一个注释,说“已放置标记”。
4个回答

36
  1. add UILongPressGestureRecognizer to your MapView

    var uilgr = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
    uilgr.minimumPressDuration = 2.0
    
    map.add (uilgr)
    
    //IOS 9
    map.addGestureRecognizer(uilgr) 
    
  2. 在长按检测功能上添加注释 - func:

  3. func addAnnotation(gestureRecognizer:UIGestureRecognizer){
        if gestureRecognizer.state == UIGestureRecognizerState.Began {
            var touchPoint = gestureRecognizer.locationInView(map)
            var newCoordinates = map.convertPoint(touchPoint, toCoordinateFromView: map)
            let annotation = MKPointAnnotation()
            annotation.coordinate = newCoordinates
    
            CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude), completionHandler: {(placemarks, error) -> Void in
                if error != nil {
                    println("Reverse geocoder failed with error" + error.localizedDescription)
                    return
                }
    
            if placemarks.count > 0 {
                let pm = placemarks[0] as! CLPlacemark
    
                // not all places have thoroughfare & subThoroughfare so validate those values
                annotation.title = pm.thoroughfare + ", " + pm.subThoroughfare
                annotation.subtitle = pm.subLocality
                self.map.addAnnotation(annotation)
                println(pm)
            }
            else {
                annotation.title = "Unknown Place"
                self.map.addAnnotation(annotation)
                println("Problem with the data received from geocoder")
            }
            places.append(["name":annotation.title,"latitude":"\(newCoordinates.latitude)","longitude":"\(newCoordinates.longitude)"])
        })
    }
    }
    
  4. 或者您可以添加没有标题的注释:

  5. func action(gestureRecognizer:UIGestureRecognizer){
        var touchPoint = gestureRecognizer.locationInView(map)
        var newCoordinates = map.convertPoint(touchPoint, toCoordinateFromView: map)
        let annotation = MKPointAnnotation()
        annotation.coordinate = newCoordinates
        map.addAnnotation(annotation)
    }
    

9
在iOS 9中,似乎应该将 map.add(uilgr) 改为 map.addGestureRecognizer(uilgr) - Ri_

11

1) 实例化一个 UILongPressGestureRecognizer 并将其添加到 MKMapView 中。

2) 当用户长按后选择器被调用时,使用适当的标题和坐标调用 MKMapView 中的 addAnnotation 方法

3) 然后确保符合 MKMapViewDelegate 并实现 viewForAnnotation:,该方法将在您添加注释后立即调用并返回一个 MKPinAnnotationView


3
第二步:我该如何找到适当的坐标? - Matt Spoon
4
convertPoint(_:toCoordinateFromView:) 方法可以将从 UILongPressGestureRecognizer 获取的 CGPoint 转换为 MKMapView 中的实际坐标。 - Josh Hamet
convertPoint必须在MKMapView本身上使用,而不是在“常规”ViewController上使用。 - user5306470

7

首先在 viewDidLoad 中声明 UIGestureRecognizer

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(addWaypoint(longGesture:)))
mapView.addGestureRecognizer(longGesture)

其次添加长按功能函数

@objc func addWaypoint(longGesture: UIGestureRecognizer) {

    let touchPoint = longGesture.location(in: mapView)
    let wayCoords = mapView.convert(touchPoint, toCoordinateFrom: mapView)
    let location = CLLocation(latitude: wayCoords.latitude, longitude: wayCoords.longitude)
    myWaypoints.append(location)

    let wayAnnotation = MKPointAnnotation()
    wayAnnotation.coordinate = wayCoords
    wayAnnotation.title = "waypoint"
    myAnnotations.append(wayAnnotation)
}

我建议创建一个数组来存储注释,以便在需要删除时更加方便,如下所示...

var myAnnotations = [MKPointAnnotation]()

如果您有不同的注释,可以仅删除您想要的注释,方法是在添加新注释时将其添加到数组中。要仅删除一组注释,请执行以下操作:

for dots in myAnnotations{
    mapView.removeAnnotation(dots)
}

要删除所有注释,请尝试使用以下方法:

mapView.removeAnnotations(mapView.annotations)

很抱歉的是翻译方面可能有不妥之处...


你不能将MKPointAnnotation附加到CLLocation数组中。请编辑你的答案。 - Dohab

2

更新Swift3

func action(gestureRecognizer:UIGestureRecognizer){
    let touchPoint = gestureRecognizer.location(in: mapView)
    let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView)
    let annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinates
    mapView.addAnnotation(annotation)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接