我是SwiftUI的新手,正在尝试通过GoogleMapsApi的地图实施解决方案,以便用户可以触摸地图并执行操作。为此,我知道必须实现委托,但是我无法弄清楚如何使用SwifUI来实现。在Web中,甚至在Swift甚至是Objective C中,都有很多代码示例,但在SwifUI上找不到任何代码示例。
这就是我所做的(我试图使这段代码尽可能简单):
struct GoogleMapsHomeView: UIViewRepresentable {
func makeUIView(context: Self.Context) -> GMSMapView {
let mapView = GMSMapView.map()
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: Context) {
}
}
struct HomeView: View {
var body: some View {
GoogleMapsHomeView()
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
有人可以帮助我声明GMSMapViewDelegate和相关的侦听器以进行用户地图移动检测吗?
提前10倍寻求帮助。
常见的模式是使用协调器作为代理
struct GoogleMapsHomeView: UIViewRepresentable {
func makeUIView(context: Self.Context) -> GMSMapView {
let mapView = GMSMapView.map()
mapView.delegate = context.coordinator
return mapView
}
func makeCoordinator() -> Coordinator {
Coordinator(owner: self)
}
func updateUIView(_ mapView: GMSMapView, context: Context) {
}
class Coordinator: NSObject, GMSMapViewDelegate {
let owner: GoogleMapsHomeView // access to owner view members,
init(owner: GoogleMapsHomeView) {
self.owner = owner
}
// ... delegate methods here
}
}