如何在 google-maps swift ui 中向标记添加自定义信息窗口?

问题描述

我试图在 SwiftUi 中创建一个类似于 bellow 的视图,但没有成功Customized info window swift ui

解决方法

由于这个问题没有太多细节,我将放弃一些假设。首先,我假设您通过 UIViewControllerRepresentable 调用 MapView。

我对 Google Maps SDK 不太熟悉,但这可以通过 GMSMapViewDelegate 方法实现。实现正确的 GMSMapViewDelegate 方法后,您可以使用 ZStacks 来呈现您想要显示的图像。

例如:

struct MapView: UIViewControllerRepresentable {
    var parentView: ContentView
    func makeUIViewController(context: Context) {
        let mapView = GMSMapView()
        return mapView
    }
    func updateUIViewController(_ uiViewController: GMSMapView,context: Context) {
        
    }
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    class Coordinator: NSObject,GMSMapViewDelegate {
        var parent: MapView
        init(_ parent: MapView) {
            self.parent = parent
        }
        //Use the proper Google Maps Delegate method to find out if a marker was tapped and then show the image by doing: parent.parentView.isShowingInformationImage = true. 
    }
}

在您希望将此 MapView 放入的 SwiftUI 视图中,您可以执行以下操作:

struct ContentView: View {
    @State var isShowingInformationImage = false
    var body: some View {
        ZStack {
            if isShowingInformationImage {
                //Call the View containing the image
            }
            MapView(parentView: self)
        }
    }
}