I have just started learning Swift.
Question:
When I touch on map to place the pin annotation and drag my finger it creates repeating line of annotation.
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.
}
}
Xcode 8.2 • Swift 3.0.2
You just need to check the gesture recognizer state and make sure if not .Began return. Just add this if condition at the top of your action method:
If you would like to allow the user to move the pin while touching you will need to switch the gesture recognizer state and update the annotation coordinate if gestureRecognizer.state changes: