2016-11-28 75 views
1

我到目前为止所做的是创建一个地图。然后显示用户位置和中心,它使行驶(汽车等)用注释针创建长按手势识别器

时地图居中但现在我想补充一个长按手势,这样如果用户不输入引脚将被丢弃。我一直在努力处理教程和模拟器崩溃。

我怎么想补充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) 
    } 
} 

回答

5

所有的第一个PIN码的斯威夫特3签名CLLocationManagerDelegate方法的locationManager(_:didUpdateLocations:)改变,所以你需要改变方法如下。

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

您可以在viewDidLoadmapView使用longGesturemapView这样,第一addGestureRecognizer

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

感谢我似乎在这条线被歌厅此错误 让longPressGesture = UILongPressGestureRecognizer(目标:自我,动作:#selector(addAnnotationOnLongPress(手势:))) 使用局部变量addAnnotationOnLongPress的”(手势:)”其声明 – sabrefm1

+0

之前没有后顾之忧我sorrted – sabrefm1

+0

唯一的事情就是现在看到的是,坐标记录在输出,但@ sabrefm1你有没有加入'addAnnotation'的代码它不是丢弃任何引脚 – sabrefm1

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

@RohirParsana出色答卷。但是,我如何获得经度和纬度?我想把这些变成变量。最好,菲利普 刚刚得到它:-) touchCoordinate返回经度和纬度。我可以单独获得这些信息。打印(touchCoordinate.latitude) – PhilipS

0

我认为这等小更新代码的帮助来实现与放脚的注释中长按:

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


    }