在单独的视图控制器中更新地图视图数据

Update mapview data in separate view controller

注意:您无需熟悉 Google Maps SDK 或 Places SDK 即可回答此问题。 您好,我正在使用 GMSMapView 在我的主要 MapViewControllerGMSPlacesClient 上从子视图控制器上的 Places SDK 搜索和 return 地点列表。 像这样:

我要做的是将mapView的相机目标坐标(屏幕上地图的中心)发送给子视图控制器。基本上我只是想将数据传递给下一个视图控制器。

到目前为止,我有这个:

在我的子视图控制器中:

let mapViewController = MapViewController()

 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
         searching = true
       
        
        let target = mapViewController.mapView.camera.target
        
        let northeast = CLLocationCoordinate2D(latitude: target.latitude + 0.1, longitude: target.longitude + 0.1)
      let southwest = CLLocationCoordinate2D(latitude: target.latitude - 0.1, longitude: target.longitude - 0.1)
       
        
      filter.locationBias = GMSPlaceRectangularLocationOption(northeast, southwest)
        
        GMSPlacesClient.shared().findAutocompletePredictions(fromQuery: searchText, filter: filter, sessionToken: nil, callback: { (result, error) in
            if error == nil && result != nil {
                self.matchingItems = result!
               
            }
        })
        mapSearchTableView.reloadData()
       
        
}

并在 MapViewController

 @IBOutlet weak var mapViewView: UIView!
    var mapView = GMSMapView()

 override func viewDidLoad() {
        super.viewDidLoad()
       
        locationManager.delegate = self
        
        if CLLocationManager.locationServicesEnabled() {
            
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
        else {
            locationManager.requestWhenInUseAuthorization()
        }
        
        
        let camera = GMSCameraPosition.camera(withLatitude: 39.8097343, longitude: -98.5556199, zoom: 3.0)
        mapView = GMSMapView.map(withFrame: self.view.frame, camera: camera)
        mapViewView.addSubview(mapView)
        
        
        mapView.delegate = self
        mapView.isMyLocationEnabled = true

}

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     
        let location = locations.last

        let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 13.0)

        self.mapView.animate(to: camera)
        
        self.locationManager.stopUpdatingLocation()
        
    }

这似乎不起作用,因为当我在子视图控制器中打印 mapViewController.mapView.camera.target(或来自 MapViewControllermapView 的中心坐标时)显示坐标 (0,0)。我还尝试了许多其他方法,包括在 MapViewController 本身中设置 target,然后从子视图控制器调用 target,但子视图控制器只读取初始化坐标target,而不是我想要的更新坐标。

所以基本上,我想问的是是否有办法在子视图控制器中不断更新 mapView 的相机位置坐标(目标)。知道的请告诉我!

我解决了!所要做的就是在原始控制器中添加一个 struct - MapViewController 包含目标变量,如下所示:

 struct setTarget {
        static var target = CLLocationCoordinate2D()
    }

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
        setTarget.target = mapView.camera.target
    }

每次更新位置时,我都将目标设置为 mapView.camera.target,然后每次需要时,我都会从我的子视图控制器调用 MapViewController.setTarget.target