【问题标题】:Update mapview data in separate view controller在单独的视图控制器中更新地图视图数据
【发布时间】:2021-01-25 04:41:14
【问题描述】:

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

我要做的是将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 打印 mapViewController.mapView.camera.target(或 mapView 的中心坐标)时,在子视图控制器中它显示的坐标为 (0, 0)。我还尝试了许多其他方法,包括在MapViewController 本身内设置target,然后从子视图控制器调用target,但子视图控制器只读取target 的初始化坐标,而不是更新后的坐标,就像我想要的那样。

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

【问题讨论】:

    标签: ios swift google-places-api gmsmapview


    【解决方案1】:

    我解决了!只需在原始控制器中添加一个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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多