iosswiftlong-pressuilongpressgesturerecogni

Longpress gestureRecognizer producing duplicate locations (SWIFT)


I am making an app where you can add pins to map locations via a long press. However, the long press appears to be duplicating the locations. Here is my code:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let userLocation = locations[0]

if activePlace == -1 {

    latitude = userLocation.coordinate.latitude

    longitude = userLocation.coordinate.longitude

} else {

    latitude = Double(latitudePassed)!

    longitude = Double(longitudePassed)!

}



let latDelta : CLLocationDegrees = 0.05

let lonDelta : CLLocationDegrees = 0.05

let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)

let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

let region = MKCoordinateRegion(center: location, span: span)

map.setRegion(region, animated: true)

let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:)) )

uilpgr.minimumPressDuration = 2

map.addGestureRecognizer(uilpgr)


}

@objc func longpress(gestureRecognizer: UIGestureRecognizer) {

let touchpoint = gestureRecognizer.location(in: self.map)

print(touchpoint)

let coordinate = map.convert(touchpoint, toCoordinateFrom: self.map)

let annotation = MKPointAnnotation()

annotation.coordinate = coordinate

annotation.title = "New Place"

let annotationLat = coordinate.latitude

let annotationLon = coordinate.longitude

places.append(["name": annotation.title!, "latitude": String(annotationLat), "longitude": String(annotationLon)])

map.addAnnotation(annotation)

}

As you can see, I'm printing the touchpoint at the beginning of the function and I'm getting the same location printing several times - sometimes twice, sometimes up to 12 times. I've trawled StackOverflow and cannot find a similar issue... any help would be greatly appreciated.


Solution

  • Long-press gestures are continuous.

    Try with the .began state, something like this:

    @objc func longpress(gestureRecognizer: UIGestureRecognizer) {
    
        if gestureRecognizer.state == .began {
    
            let touchpoint = gestureRecognizer.location(in: self.map)
    
            print(touchpoint)
    
            let coordinate = map.convert(touchpoint, toCoordinateFrom: self.map)
    
            let annotation = MKPointAnnotation()
    
            annotation.coordinate = coordinate
    
            annotation.title = "New Place"
    
            let annotationLat = coordinate.latitude
    
            let annotationLon = coordinate.longitude
    
            places.append(["name": annotation.title!, "latitude": String(annotationLat), "longitude": String(annotationLon)])
    
            map.addAnnotation(annotation)
        }
    }
    

    Also try to trigger addGestureRecognizer only once, maybe in viewDidLoad where you setup/initiate map, so this should be just in viewDidLoad for example and fired once your map has been initialized:

    let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longpress(gestureRecognizer:)) )
    uilpgr.minimumPressDuration = 2
    map.addGestureRecognizer(uilpgr)
    

    To know more about UIGestureRecognizer States follow Apple's documentation: https://developer.apple.com/documentation/uikit/uigesturerecognizer/state