Swift-GMSMapView animate(toLocation :) / animate(to:CameraPositon)不起作用

问题描述 投票:0回答:2

这是我初始化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)
    }
}

我正在尝试将相机重新定位到某些坐标,但是什么也没有发生。我已经尝试过stackoverflowgoogle的所有解决方案。

我在lat中有lnglocation-已选中。该函数称为-已选中。

我也尝试过self.view.layoutSubviews()self.view.layoutIfNeeded(),也尝试map

这是我初始化地图的方式-GMSMapView类ViewController:UIViewController {var map = GMSMapView()覆盖func viewDidLoad(){super.viewDidLoad()setupGoogleView()} ...

ios swift xcode gmsmapview
2个回答
0
投票
我认为问题在于出于相同的目的调用两个函数,因为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) } }


0
投票
尝试使用CLLocationManagerDelegate

// 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() } }

© www.soinside.com 2019 - 2024. All rights reserved.