1

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

但是现在我想添加一个长按手势,这样如果用户输入了一个 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)
    }
}
4

3 回答 3

7

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

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

你可以像这样使用longGesturewith mapView,首先addGestureRecognizer在你mapViewviewDidLoad.

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)
    }
}
于 2016-11-28T12:50:36.917 回答
1

我认为这个更新的小代码有助于其他人通过长按放置引脚来实现注释:

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


    }
于 2017-09-09T17:06:42.907 回答
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
  }
}
于 2016-11-28T12:49:36.887 回答