Как удалить последнюю аннотацию в MapView через кнопку? - PullRequest
0 голосов
/ 01 октября 2019
@IBAction 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 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)
    }
}

 @IBAction func deleteLast(sender: UIButton){

}

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

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