我在地图(位于我的 ContentView 上)中设置了 3 个标记(0、1、2)。现在我只想删除标记 0(不是 1 也不是 2)。
我发现的只是“removeAnnotation”,尽管我的标记只是一个标记,没有声明为注释。尽管如此,我从堆栈溢出中得到了答案(https://stackoverflow.com/questions/47959193/swift-4-to-remove-only-one-annotation-not-all/47965214,但它不起作用.
我是 Swift 的初学者,所以问题似乎(除了 removeAnnotation-Question 之外)出在我的代码本身中。
我的代码:
import SwiftUI
import MapKit
struct MyLocationsArray: Identifiable {
let id = UUID()
let number: Int
let name: String
let label: String
let coordinates: CLLocationCoordinate2D
}
struct ContentView: View {
@State var myLocs = [
MyLocationsArray(number: 0, name: "name A", label: "label A", coordinates: CLLocationCoordinate2D(latitude: 39.873563, longitude: 3.170857999999953)),
MyLocationsArray(number: 1, name: "name B", label: "label B", coordinates: CLLocationCoordinate2D(latitude: 39.654235, longitude: 2.947624)),
MyLocationsArray(number: 2, name: "name C", label: "label C", coordinates: CLLocationCoordinate2D(latitude: 39.530, longitude: 2.90))
]
var body: some View {
Map()
{
Marker(myLocs[0].name, coordinate: myLocs[0].coordinates)
Marker(myLocs[1].name, coordinate: myLocs[1].coordinates)
Marker(myLocs[2].name, coordinate: myLocs[2].coordinates)
}
Button("Button title") {
removeSpecificAnnotation()
}
}
func removeSpecificAnnotation() {
for n in myLocs {
if let name = n.name, name == "name A" { // "Conditioner for conditional binding must have Optional type, not 'String'": But my "name" ist a String.
removeAnnotation(n) // "Cannot find removeAnnotation in Scope": But with whatelse can I remove a Marker?
}
}
}
}
我在 stackoverflow 中搜索并找到了答案,但不知何故它不适用于我的代码。在互联网上,除了将图钉颜色更改为透明之外,我没有找到“如何删除标记”的答案。答,正如我所说,这也可能是我的通用代码,因为我是初学者。
在 SwiftUI 中,您可以使用
data
来实现您想要的。
因此请尝试使用
data
( myLocs) 选择然后删除所选的方法
MyLocation
,如示例代码所示。
struct MyLocation: Identifiable, Hashable {
let id = UUID()
var number: Int
var name: String
var label: String
var latitude: Double
var longitude: Double
var coordinates: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
init(number: Int, name: String, label: String, coordinates: CLLocationCoordinate2D) {
self.number = number
self.name = name
self.label = label
self.latitude = coordinates.latitude
self.longitude = coordinates.longitude
}
}
struct ContentView: View {
@State private var myLocs = [
MyLocation(number: 0, name: "name A", label: "label A", coordinates: CLLocationCoordinate2D(latitude: 39.873563, longitude: 3.170857999999953)),
MyLocation(number: 1, name: "name B", label: "label B", coordinates: CLLocationCoordinate2D(latitude: 39.654235, longitude: 2.947624)),
MyLocation(number: 2, name: "name C", label: "label C", coordinates: CLLocationCoordinate2D(latitude: 39.530, longitude: 2.90))
]
@State private var selection: MyLocation?
var body: some View {
Map(selection: $selection) {
ForEach(myLocs) { loc in
Marker(loc.name, coordinate: loc.coordinates).tag(loc)
}
}
Button("Remove selected") {
removeSelectedMarker()
}
}
func removeSelectedMarker() {
if let index = myLocs.firstIndex(where: {$0.id == selection?.id}){
myLocs.remove(at: index)
}
}
}