在swift中长按时将图钉注释添加到地图视图中

时间:2015-06-16 03:38:49

标签: ios swift annotations maps mkmapview

我正在尝试制作一款iPhone应用程序,要求用户能够长时间按下地图视图上的某个位置以放置一个针脚。有人知道这是怎么做的吗?

当您长按屏幕时,可以在Apple地图中观察到该行为。它将丢弃一个引脚并提供一个注释“drop pin”

4 个答案:

答案 0 :(得分:28)

  1. UILongPressGestureRecognizer添加到MapView

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

    上添加注释
    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)"])
        })
    }
    }
    
  3. 或者您可以添加没有任何标题的注释:

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

答案 1 :(得分:11)

1)实例化UILongPressGestureRecognizer并将其添加到MKMapView

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

3)然后确保您符合MKMapViewDelegate并实施viewForAnnotation:,您将在添加注释后立即调用MKPinAnnotationView

答案 2 :(得分:5)

首先在UIGestureRecognizer

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

第二次添加longPress函数

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

我建议在数组中创建注释,如果你想删除它,将在以后为你服务,就像这样......

var myAnnotations = [CLLocation]()

如果您有不同的注释,则只能删除所需的注释,以便在向阵列添加新的注释添加时。要仅删除一组注释,请执行以下操作

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

要删除所有注释,请尝试

mapView.removeAnnotations(mapView.annotations)

道歉......

答案 3 :(得分: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)
}