Как я могу остановить формирование нескольких аннотаций одним нажатием? - PullRequest
0 голосов
/ 29 сентября 2019
func addPin(_ sender: UILongPressGestureRecognizer) {


let location = sender.location(in: self.mapView)
let locCoord = self.mapView.convert(location, toCoordinateFrom: self.mapView)

let annotation = MKPointAnnotation()

annotation.coordinate = locCoord
annotation.title = titleTextField.text

var annotationsArray: [[String:Any]]!
var annotationsData = UserDefaults.standard.data(forKey: "StoredAnnotations")

//If the data is nil, then set the new annotation as the only element in the array
if annotationsData == nil {
    annotationsArray = [newAnnotationDict]
} else {
    //If it isn't nil, then convert the data into an array of dicts
    do {
        //Convert this data into an array of dicts
        annotationsArray = try JSONSerialization.jsonObject(with: annotationsData!, options: []) as! [[String:Any]]
        annotationsArray.append(newAnnotationDict)
    } catch {
        print(error.localizedDescription)
    }

}

do {

    //Use JSONSerialization to convert the annotationsArray into Data
    let jsonData = try JSONSerialization.data(withJSONObject: annotationsArray, options: .prettyPrinted)

    //Store this data in UserDefaults
    UserDefaults.standard.set(jsonData, forKey: "StoredAnnotations")
} catch {
    print(error.localizedDescription)
}

В настоящее время я создаю аннотации на карте следующим образом.Однако, когда пользователь нажимает, чтобы создать аннотацию и случайно перетаскивает свой палец, на карте создается много нежелательных аннотаций.Я бы хотел, чтобы пользователь поднял палец и снова нажал, чтобы создать вторую аннотацию.Как мне это исправить?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...