SwiftUI Map 混合注释
SwiftUI Map mixing up annotations
我已将我的地图视图连接到一个自定义数组 Product
objects,它存储每个注释的相关详细信息(自定义注释称为 MapAnnotationButton
)。
有可能进行搜索,每次将搜索请求发送到我的后端时,响应都会被转换为 Product
objects 并保存到数组中(数组的先前内容正在被清除)。然而,出于某种原因,在第一次搜索后,我的地图注释混淆了。每次搜索(相同的短语)时,它都会被打乱。虽然从来没有第一次。
我已经检查过,但即使在注释创建块中,坐标和名称及其他属性也匹配。
import SwiftUI
import MapKit
struct MapView: View {
@State var keyword = ""
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
@State var products: [Product] = []
var body: some View {
ZStack(alignment: Alignment(horizontal: .center, vertical: .top), content: {
Map(coordinateRegion: $region, annotationItems: self.products, annotationContent: { current_item in
MapAnnotation(coordinate: current_item.location?.CLcoordinates ?? CLLocationCoordinate2D(latitude: 1000, longitude: 1000)) {
MapAnnotationButton(item: current_item)
}
})
TextField("Search", text: $keyword, onEditingChanged: { editing in
}, onCommit: {
UIApplication.shared.endEditing()
BackendClient.shared.query(keyword: keyword) { products, success in
self.products = products ?? []
}
})
})
}
}
我的解释是,当注释创建块被调用时,根据其内容,products
中包含的项目数量约为 2 到 4 倍,正确的注释上方可能会有注释。这意味着它看起来只是混淆了,而实际上只有其他注释阻止了下面的正确注释。
然而,每个注释实际上都应该在其正确的位置,绝不允许发生这样的事情。
非常感谢您的提示和想法!
谢谢!
第一次搜索:
二次搜索:
据我所知,SwiftUI 框架中似乎存在错误。出于某种原因,如果视图位于单独的结构中,它们将不会更新。如果我从 MapAnnotationButton 直接复制到 MapAnnotation 视图块中,它一切正常。
雷达已经备案了
谢谢你的评论!
我已将我的地图视图连接到一个自定义数组 Product
objects,它存储每个注释的相关详细信息(自定义注释称为 MapAnnotationButton
)。
有可能进行搜索,每次将搜索请求发送到我的后端时,响应都会被转换为 Product
objects 并保存到数组中(数组的先前内容正在被清除)。然而,出于某种原因,在第一次搜索后,我的地图注释混淆了。每次搜索(相同的短语)时,它都会被打乱。虽然从来没有第一次。
我已经检查过,但即使在注释创建块中,坐标和名称及其他属性也匹配。
import SwiftUI
import MapKit
struct MapView: View {
@State var keyword = ""
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
@State var products: [Product] = []
var body: some View {
ZStack(alignment: Alignment(horizontal: .center, vertical: .top), content: {
Map(coordinateRegion: $region, annotationItems: self.products, annotationContent: { current_item in
MapAnnotation(coordinate: current_item.location?.CLcoordinates ?? CLLocationCoordinate2D(latitude: 1000, longitude: 1000)) {
MapAnnotationButton(item: current_item)
}
})
TextField("Search", text: $keyword, onEditingChanged: { editing in
}, onCommit: {
UIApplication.shared.endEditing()
BackendClient.shared.query(keyword: keyword) { products, success in
self.products = products ?? []
}
})
})
}
}
我的解释是,当注释创建块被调用时,根据其内容,products
中包含的项目数量约为 2 到 4 倍,正确的注释上方可能会有注释。这意味着它看起来只是混淆了,而实际上只有其他注释阻止了下面的正确注释。
然而,每个注释实际上都应该在其正确的位置,绝不允许发生这样的事情。
非常感谢您的提示和想法! 谢谢!
第一次搜索:
二次搜索:
据我所知,SwiftUI 框架中似乎存在错误。出于某种原因,如果视图位于单独的结构中,它们将不会更新。如果我从 MapAnnotationButton 直接复制到 MapAnnotation 视图块中,它一切正常。
雷达已经备案了
谢谢你的评论!