如何在iOS中点击Google地图标记信息窗口后使表单表出现?



我目前正在使用SwiftUI和Google Maps构建一个应用程序。我正在尝试在点击谷歌地图标记的信息窗口后显示表单表,但我无法使其工作。

在我的应用程序的其他部分中,我使用此方法显示工作表: 此处的示例方法

我尝试使用上面相同的方法在点击标记的信息窗口后显示工作表,但在函数中执行此操作时遇到问题。我下面的代码片段提供了更多详细信息。

-

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

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
}

我上面的函数目前在点击信息窗口时显示一个视图,但它只出现在我的谷歌地图视图上,所以我没有得到动画,也无法像典型的 iOS 表单表那样关闭视图。

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

您好,从结构视图与UIViewController进行交互,您需要绑定一个变量..我们声明@Binding var isClicked : Bool和 如果您需要将更多参数传递给结构体,则需要通过公告@Binding声明它。 UIViewController 中的任何方式都会显示isClicked属性"self.isClicked"未初始化以解决此问题,我们声明:

@Binding var isClicked
init(isClicked: Binding<Bool>) {
_isClicked = isClicked
super.init(nibName: nil, bundle: nil) 
}

UIViewController 的指定初始化器也是 initWithNibName:bundle:。你应该改称它。如果您没有笔尖,请为 nibName 传递 nil(捆绑也是可选的(。 现在我们已经为UIViewController设置了所有设置,我们转到UIViewControllerRepresentable:与我们最初所做的相同,我们需要声明 var isClicked @Binding因为 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
}

相关内容

  • 没有找到相关文章

最新更新