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

时间:2016-11-28 12:27:10

标签: swift swift3 mkmapview xcode8 uilongpressgesturerecogni

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

但是现在我想添加一个长按手势,这样如果用户输入一个引脚就会被丢弃。我一直在努力学习教程和模拟器崩溃。

我如何添加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)
    }
}

3 个答案:

答案 0 :(得分:5)

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

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

您可以longGesture使用mapView addGestureRecognizermapView viewDidLoad中的let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotationOnLongPress(gesture:))) longPressGesture.minimumPressDuration = 1.0 self.mapView.addGestureRecognizer(longPressGesture)

UILongPressGestureRecognizer

现在为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) } } 添加操作。

percentage = ((score * 100)  / 3 );

答案 1 :(得分:1)

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

添加点按手势

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

答案 2 :(得分:1)

我认为这个更新的小代码可以帮助其他人在长按时使用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.
        }


    }