Ios 在地图上重复pin注释
我刚开始学斯威夫特 问题: 当我触摸地图以放置pin注释并拖动手指时,它会创建注释的重复线Ios 在地图上重复pin注释,ios,swift,swift2,apple-maps,Ios,Swift,Swift2,Apple Maps,我刚开始学斯威夫特 问题: 当我触摸地图以放置pin注释并拖动手指时,它会创建注释的重复线 Xcode 8.2•Swift 3.0.2 你只需要检查手势识别器的状态,如果没有,请确认。开始返回。只需在操作方法的顶部添加以下if条件: func action(_ gestureRecognizer: UIGestureRecognizer) { if gestureRecognizer.state != UIGestureRecognizerState.began {
Xcode 8.2•Swift 3.0.2 你只需要检查手势识别器的状态,如果没有,请确认。开始返回。只需在操作方法的顶部添加以下if条件:
func action(_ gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state != UIGestureRecognizerState.began {
return
}
// your code
如果要允许用户在触摸时移动pin,则需要切换手势识别器状态,并在GestureRecognitor.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")
}
}
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")
}
}