Ios 如何删除地图视图上的最后一个注释?
}Ios 如何删除地图视图上的最后一个注释?,ios,swift,mapkit,mapkitannotation,Ios,Swift,Mapkit,Mapkitannotation,} 我目前正在使用上面给出的代码在MapView上创建注释。我想删除用户创建的上一个批注。如何使用deleteLast按钮执行此操作?尝试注释array.removeLast()@KosukeOgawa如何通过按钮执行此操作。我试图将annotationsArray变量设置为全局变量,但注释出现了几次故障。。。还有别的办法吗? @IBAction func addPin(_ sender: UILongPressGestureRecognizer) { let location = send
我目前正在使用上面给出的代码在MapView上创建注释。我想删除用户创建的上一个批注。如何使用deleteLast按钮执行此操作?尝试
注释array.removeLast()
@KosukeOgawa如何通过按钮执行此操作。我试图将annotationsArray变量设置为全局变量,但注释出现了几次故障。。。还有别的办法吗?
@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 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)
}
}
@IBAction func deleteLast(sender: UIButton){