这是我初始化map
-GMSMapView
的方式>
class ViewController: UIViewController { var map = GMSMapView() override func viewDidLoad() { super.viewDidLoad() setupGoogleView() } private func setupGoogleView() { guard let coordinates = getUserLocation() else { return } let camera = GMSCameraPosition.camera(withLatitude: coordinates.latitude, longitude: coordinates.longitude, zoom: 16.0) self.map = GMSMapView.map(withFrame: CGRect.zero, camera: camera) map.settings.tiltGestures = false map.mapType = .satellite map.delegate = self map.frame = view.frame self.view.addSubview(map) } }
问题出在我从文件的其他地方调用此函数时
private func animateTo(location: CLLocationCoordinate2D) { DispatchQueue.main.async { let cameraPosition = GMSCameraPosition(target: location, zoom: 20) self.map.animate(toLocation: location) self.map.animate(to: cameraPosition) } }
我正在尝试将相机重新定位到某些坐标,但是什么也没有发生。我已经尝试过
stackoverflow
和
我在lat
中有lng
和location
-已选中。该函数称为-已选中。
我也尝试过self.view.layoutSubviews()
,self.view.layoutIfNeeded()
,也尝试map
这是我初始化地图的方式-GMSMapView类ViewController:UIViewController {var map = GMSMapView()覆盖func viewDidLoad(){super.viewDidLoad()setupGoogleView()} ...
self.map.animate(to: cameraPosition)
使位置上的地图动画化,与self.map.animate(to: cameraPosition)
相同>仅尝试拨打self.map.animate(to: cameraPosition)
:
private func animateTo(location: CLLocationCoordinate2D) {
let cameraPosition = GMSCameraPosition.camera(withTarget: location, zoom: 20.0)
DispatchQueue.main.async {
self.map.animate(to: cameraPosition)
}
}
// 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.")
getFilialList()
@unknown default:
fatalError()
}
}