如何在iOS中点击Google Maps标记infoWindow后显示表单?

问题描述 投票:0回答:1

我目前正在使用SwiftUI和Google Maps构建应用程序。我正在尝试点击Google Maps标记的infoWindow后显示一个表单,但是我无法使其正常工作。

在我应用的其他部分,我使用以下方法显示表格:Example method here

我尝试使用与上面相同的方法在点击标记的infoWindow后显示工作表,但是在函数内部执行此操作时遇到了麻烦。我的以下代码段提供了更多详细信息。

-

以下是我的GMView.swift文件的精简版本,该文件控制着我的Google Maps实例。 (我的文件看起来与典型的Swift + Google地图集成不同,因为我使用的是SwiftUI)。您会注意到文件的3个主要部分:1。视图,2。 GMController类和3。 GMControllerRepresentable结构:

import SwiftUI
import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation
import Foundation



struct GoogMapView: View {
    var body: some View {
        GoogMapControllerRepresentable()
    }
}


class GoogMapController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
    var locationManager = CLLocationManager()
    var mapView: GMSMapView!
    let defaultLocation = CLLocation(latitude: 42.361145, longitude: -71.057083)
    var zoomLevel: Float = 15.0
    let marker : GMSMarker = GMSMarker()


    override func viewDidLoad() {
        super.viewDidLoad()

//        Control location data
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.distanceFilter = 50
            locationManager.startUpdatingLocation()
        }


        let camera = GMSCameraPosition.camera(withLatitude: defaultLocation.coordinate.latitude, longitude: defaultLocation.coordinate.longitude, zoom: zoomLevel)
        mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        mapView.isMyLocationEnabled = true
        mapView.setMinZoom(14, maxZoom: 20)
        mapView.settings.compassButton = true
        mapView.isMyLocationEnabled = true
        mapView.settings.myLocationButton = true
        mapView.settings.scrollGestures = true
        mapView.settings.zoomGestures = true
        mapView.settings.rotateGestures = true
        mapView.settings.tiltGestures = true
        mapView.isIndoorEnabled = false


        marker.position = CLLocationCoordinate2D(latitude: 42.361145, longitude: -71.057083)
        marker.title = "Boston"
        marker.snippet = "USA"
        marker.map = mapView


//        view.addSubview(mapView)
        mapView.delegate = self
        self.view = mapView

    }

    // Handle incoming location events.
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      let location: CLLocation = locations.last!
      print("Location: \(location)")
    }

    // Handle authorization for the location manager.
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
      switch status {
      case .restricted:
        print("Location access was restricted.")
      case .denied:
        print("User denied access to location.")
        // Display the map using the default location.
        mapView.isHidden = false
      case .notDetermined:
        print("Location status not determined.")
      case .authorizedAlways: fallthrough
      case .authorizedWhenInUse:
        print("Location status is OK.")
      }
    }

    // Handle location manager errors.
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
      locationManager.stopUpdatingLocation()
      print("Error: \(error)")
    }

}


struct GoogMapControllerRepresentable: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<GMControllerRepresentable>) -> GMController {
        return GMController()
    }

    func updateUIViewController(_ uiViewController: GMController, context: UIViewControllerRepresentableContext<GMControllerRepresentable>) {

    }
}

这是我已经在我的GMView.swift文件中添加到GMController类的函数,上面的Google文档说该函数用于在点击标记的infoWindow时进行处理:

// Function to handle when a marker's infowindow is tapped
    func mapView(_ mapView: GMSMapView, didTapInfoWindowOf didTapInfoWindowOfMarker: GMSMarker) {
        print("You tapped a marker's infowindow!")
//        This is where i need to get the view to appear as a modal, and my attempt below
        let venueD2 = UIHostingController(rootView: VenueDetail2())
        venueD2.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 48)
        self.view.addSubview(venueD2.view)
        return
    }

[我上面的函数当前在轻按信息窗口时显示一个视图,但是它只是显示在我的Google Maps视图上,所以我没有得到动画,也无法像典型的iOS表单一样关闭该视图。

有人知道在SwiftUI中点击Google地图标记信息窗口而不是将其添加为子视图后,如何显示工作表吗?

ios google-maps swiftui google-maps-sdk-ios gmsmapview
1个回答
1
投票
@Binding var isClicked init(isClicked: Binding<Bool>) { _isClicked = isClicked super.init(nibName: nil, bundle: nil) }

也为UIViewController指定的初始化程序是initWithNibName:bundle:。您应该打电话给它。如果没有笔尖,则将nil传入nilName(捆绑包也是可选的)。现在我们已经为UIViewController进行了所有设置,然后移至UIViewControllerRepresentable:最初我们要做的就是声明@Binding var isClicked,因为viewController会在初始化时请求一个新参数,所以我们会得到类似这个:

@Binding var isClicked: Bool
func makeUIViewController(context: UIViewControllerRepresentableContext<GMControllerRepresentable>) -> GMController {
        return GMController(isClicked: $isClicked)
    }

在结构视图中:

@State var isClicked: Bool = false
var body: some View {
        GoogMapControllerRepresentable(isClicked: $isClicked)
.sheet(isPresented: $isShown) { () -> View in
            <#code#>
        }
    }

还有另一件事,我们只需要像这样在标记单击时切换此变量:

func mapView(_ mapView: GMSMapView, didTapInfoWindowOf didTapInfoWindowOfMarker: GMSMarker) {
        print("You tapped a marker's infowindow!")
//        This is where i need to get the view to appear as a modal, and my attempt below
       self.isClicked.toggle()
// if you want to pass more parameters you can set them from here like self.info = //mapView.coordinate <- Example
        return
    }
© www.soinside.com 2019 - 2024. All rights reserved.