[英]Update mapview data in separate view controller
Note: you don't have to be familiar with Google Maps SDK or Places SDK to answer this question.注意:您不必熟悉 Google Maps SDK 或 Places SDK 即可回答此问题。 Hi I am using a GMSMapView
on my main MapViewController
and GMSPlacesClient
from Places SDK on a child view controller to search and return a list of places.嗨,我在我的主MapViewController
和GMSPlacesClient
上使用GMSMapView
来自子视图 controller 上的 Places SDK 来搜索并返回地点列表。 Like this:像这样:
What I want to do is to send send the mapView
's camera target coordinates (the center of the map on the screen) to the child view controller.我要做的是将mapView
的相机目标坐标(屏幕上 map 的中心)发送到子视图 controller。 Basically I just want to pass data to the next view controller.基本上我只想将数据传递给下一个视图 controller。
So far, I have this:到目前为止,我有这个:
In my child view controller:在我的孩子看来 controller:
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()
}
and in MapViewController
并在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()
}
This doesn't seem to work, as when I print the mapViewController.mapView.camera.target
(or the mapView
's center coordinates from the MapViewController
), in the child view controller it shows up with coordinates of (0,0).这似乎不起作用,因为当我打印mapViewController.mapView.camera.target
(或来自MapViewController
的mapView
的中心坐标)时,在子视图 controller 中它显示的坐标为(0,0)。 I have also tried many other ways, including setting target
within the MapViewController
itself, and then calling that target
from the child view controller, but the child view controller only reads the initialized coordinate for target
, and not the updated coordinate like I want it to.我还尝试了许多其他方法,包括在MapViewController
本身中设置target
,然后从子视图 controller 调用该target
,但是子视图 controller 只读取target
的初始化坐标,而不是像我想要的那样更新坐标.
So basically, what I'm asking is if there is a way to continually update the mapView
's camera position coordinates (target) in the child view controller.所以基本上,我要问的是是否有办法在子视图 controller 中不断更新mapView
的相机 position 坐标(目标)。 Please let me know if you know!如果你知道,请告诉我!
I solved it!我解决了! All it took was adding a struct
within the original controller- the MapViewController
containing the target variable, like this:它所要做的就是在原始控制器中添加一个struct
——包含目标变量的MapViewController
,如下所示:
struct setTarget {
static var target = CLLocationCoordinate2D()
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
setTarget.target = mapView.camera.target
}
I set the target to the mapView.camera.target
every time it updated a location, and then from my child view controller, I called MapViewController.setTarget.target
every time that I needed it.每次更新位置时,我都会将目标设置为mapView.camera.target
,然后从我的子视图 controller 中,每次需要时我都会调用MapViewController.setTarget.target
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.