使用注释 pin 创建长按手势识别器

Create long press gesture recognizer with annotation pin

到目前为止我所做的是创建地图。然后显示用户位置并将其居中,以便地图在旅行(汽车等)时居中

但现在我想添加一个长按手势,这样如果用户进行输入,就会删除一个图钉。我一直在努力学习教程和模拟器崩溃。

我该如何添加 longPressGesturerecognizer 以便它在我的 mapView 上放置一个图钉。

这是我的代码-

import UIKit
import MapKit
import CoreLocation

class Page2: UIViewController, MKMapViewDelegate,  CLLocationManagerDelegate{

    @IBOutlet var mapView: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        //blue dot on the map
        self.mapView.showsUserLocation = true
        //tracking mode on
        self.mapView.userTrackingMode = .follow
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // location Manager Delegate center user on map
    private func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        let location = locations.last
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: (location?.coordinate.longitude)!)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)) //zoom on map
        self.mapView.setRegion(region, animated: true)
        self.locationManager.stopUpdatingLocation()
    }

    // print errors
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
        print("Errors: " + error.localizedDescription)
    }
}

您也可以使用点按手势放置图钉。

添加点击手势

    let tapGesture = UITapGestureRecognizer(target: self, action:#selector(AddressViewController.handleTap(_:)))
    tapGesture.delegate = self
    mapView.addGestureRecognizer(tapGesture)

手势放针

func handleTap(_ sender: UIGestureRecognizer)
{
  if sender.state == UIGestureRecognizerState.ended {

        let touchPoint = sender.location(in: mapView)
        let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
        let annotation = MKPointAnnotation()
        annotation.coordinate = touchCoordinate
        annotation.title = "Event place"
        mapView.removeAnnotations(mapView.annotations)
    mapView.addAnnotation(annotation) //drops the pin
  }
}

首先在Swift 3 中CLLocationManagerDelegate 方法的locationManager(_:didUpdateLocations:) 的签名被更改,因此您需要按如下方式更改该方法。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     //your code and don't forgot to remove private
}

您可以像这样将 longGesturemapView 一起使用,首先 addGestureRecognizer 在您的 mapView 中,在 viewDidLoad 中。

let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotationOnLongPress(gesture:)))
longPressGesture.minimumPressDuration = 1.0
self.mapView.addGestureRecognizer(longPressGesture)

现在为 UILongPressGestureRecognizer 添加操作。

@objc func addAnnotationOnLongPress(gesture: UILongPressGestureRecognizer) {

    if gesture.state == .ended {
        let point = gesture.location(in: self.mapView)
        let coordinate = self.mapView.convert(point, toCoordinateFrom: self.mapView)
        print(coordinate)
        //Now use this coordinate to add annotation on map.
        var annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        //Set title and subtitle if you want
        annotation.title = "Title" 
        annotation.subtitle = "subtitle" 
        self.mapView.addAnnotation(annotation)
    }
}

我认为这个更新的小代码可以帮助其他人通过长按 put pin 来实现注释:

    import UIKit
    import MapKit
    class ViewController: UIViewController, MKMapViewDelegate {

        @IBOutlet weak var map: MKMapView!

        override func viewDidLoad() {
            super.viewDidLoad()

            let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(gestureRecognized:)))

            //long press (2 sec duration)
            uilpgr.minimumPressDuration = 2
            map.addGestureRecognizer(uilpgr)   
        }

        func longPressed(gestureRecognized: UIGestureRecognizer){
            let touchpoint = gestureRecognized.location(in: self.map)
            let location = map.convert(touchpoint, toCoordinateFrom: self.map)

            let annotation = MKPointAnnotation()
            annotation.title = "Latitude: \(location.latitude)"
            annotation.subtitle = "Longitude: \(location.longitude)"
            annotation.coordinate = location
            map.addAnnotation(annotation)


        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


    }