2016-01-12 2 views
2

Я только что начал изучать Свифт.Повторяющаяся запись булавки на карте

Вопрос:

Когда я касаюсь на карте, чтобы разместить пин аннотацию и перетащите палец создает повторяющуюся линию аннотации.

enter image description here

import UIKit 
import MapKit 
import CoreLocation 
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 
    @IBOutlet weak var map: MKMapView! 
    var manager:CLLocationManager! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     //Manager for current location 
     manager = CLLocationManager() 
     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 
     manager.requestWhenInUseAuthorization() 
     manager.startUpdatingLocation() 
     //Getting touch Gesture to add bookmark 
     let uilpgr = UILongPressGestureRecognizer(target: self, action: "action:") 
     uilpgr.minimumPressDuration = 1 
     uilpgr.numberOfTouchesRequired = 1 
     map.addGestureRecognizer(uilpgr) 
    } 
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let userLocation:CLLocation = locations[0] 
     let latitude:CLLocationDegrees = userLocation.coordinate.latitude 
     let longitude:CLLocationDegrees = userLocation.coordinate.longitude 
     let latDelta:CLLocationDegrees = 0.002 
     let lonDelta:CLLocationDegrees = 0.002 
     let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta) 
     let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude) 
     let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span) 
     map.setRegion(region, animated: true) 
    } 
    func action(gestureRecognizer: UIGestureRecognizer) { 
     let touchPoint = gestureRecognizer.locationInView(self.map) 
     let newCoordinate: CLLocationCoordinate2D = map.convertPoint(touchPoint, toCoordinateFromView: self.map) 
     print(newCoordinate) 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = newCoordinate 
     annotation.title = "New Place" 
     annotation.subtitle = "One day I'll go here..." 
     map.addAnnotation(annotation) 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

ответ

1

Xcode 8,2 • Swift 3.0.2

Вам просто нужно проверить состояние распознавателя жест и убедитесь, что если не .Began возврата. Просто добавьте это, если условие в верхней части методы действия:

func action(_ gestureRecognizer: UIGestureRecognizer) { 

    if gestureRecognizer.state != UIGestureRecognizerState.began { 
     return 
    } 

    // your code 

Если вы хотите, чтобы позволить пользователю перемещать палец, касаясь вам нужно будет переключать состояние жеста распознавателя и обновление аннотаций координат, если gestureRecognizer .state изменения:

func action(_ gestureRecognizer: UIGestureRecognizer) { 
    switch gestureRecognizer.state { 
    case .began: 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = mapView.convert(gestureRecognizer.location(in: mapView), toCoordinateFrom: mapView) 
     annotation.title = "Untitled" 
     mapView.addAnnotation(annotation) 
    case .changed: 
     if let annotation = (mapView.annotations.filter{$0.title! == "Untitled" }).first as? MKPointAnnotation { 
      annotation.coordinate = mapView.convert(gestureRecognizer.location(in: mapView), toCoordinateFrom: mapView) 
     } 
    case .cancelled: 
     if let annotation = (mapView.annotations.filter{$0.title! == "Untitled" }).first as? MKPointAnnotation { 
      mapView.removeAnnotation(annotation) 
     } 
    // you can also prompt the user here for the annotation title 
    case .ended: 
     if let annotation = (mapView.annotations.filter{$0.title! == "Untitled" }).first as? MKPointAnnotation { 
      let alert = UIAlertController(title: "New Annotation", message: "", preferredStyle: .alert) 
      var inputTextField: UITextField? 
      alert.addAction(UIAlertAction(title: "Add", style: .default) { _ in 
       if let annotationTitle = inputTextField?.text { 
        annotation.title = annotationTitle 
        annotation.subtitle = "Lat:\(String(format: "%.06f", annotation.coordinate.latitude)) Lon:\(String(format: "%.06f", annotation.coordinate.longitude))" 
       } 
      }) 
      alert.addTextField(configurationHandler: { textField in 
       textField.placeholder = "Place Description" 
       inputTextField = textField 
      }) 
      alert.addAction(UIAlertAction(title: "Cancel", style: .cancel){ _ in 
       self.mapView.removeAnnotation(annotation) 
      }) 
      present(alert, animated: true, completion: nil) 
     } 
    default: 
     print("default") 
    } 
} 
+0

https://www.dropbox.com/s/rey43pneax8pyfe/PointAnnotation.zip?dl=0 –

+0

Свифта 3 https://www.dropbox.com/s/gyrqf661yyrfz74/PointAnnotation% 202.zip?dl=1 –

Смежные вопросы