【问题标题】:Swift - GMSMapView animate(toLocation:) / animate(to:CameraPositon) not workingSwift - GMSMapView animate(toLocation:) / animate(to:CameraPositon) 不工作
【发布时间】:2020-02-06 16:15:23
【问题描述】:

这就是我如何初始化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 上尝试了所有解决方案。

我在location 中有latlng - 检查。该函数被调用 - 检查。

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

【问题讨论】:

    标签: ios swift xcode gmsmapview


    【解决方案1】:

    尝试使用 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()
            }
        }
    

    然后

     // Handle incoming location events.
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location: CLLocation = locations.last!
            print("Location: \(location)")
            let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                                  longitude: location.coordinate.longitude,
                                                  zoom: 12)
            DispatchQueue.main.async {
                self.mapView.animate(to: camera)
                self.locationManager.stopUpdatingLocation()
            }
        }
    

    【讨论】:

    • 我正在单独处理didChangeAuthorization,我看不到那里的问题。我已将 mapView 添加到 self.view,它是可访问的,用户可以缩放和一切,但是当我在 self.view 顶部添加的自定义按钮被点击时,即使更改地图类型,实际的 mapView 也没有t 实现(就像没有收到命令)
    【解决方案2】:

    我设法解决了这个问题:

    self.view.subviews.forEach { (view) in
       if let mapView = view as? GMSMapView {
          // do update on mapView here
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 2013-12-08
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多