位置提示在 UIView 内部显示后消失

问题描述

我在使用谷歌地图和位置管理器时遇到问题。 位置提示显示不到 1 秒,然后消失,我不知道为什么,我似乎无法在 SO 上找到对该问题的任何很好的解释

我有一个包含 GooglemapsuiView 的 UIViewController。

视图控制器

class ViewController: UIViewController {

var mapsView: MapsViewController?
override func viewDidLoad() {
    super.viewDidLoad()
    setContraints()
}

private func setContraints(){
    mapsView = MapsViewController(frame: CGRect(x: 0,y: 0,width: view.frame.size.width,height: view.frame.size.height))
    mapsView!.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(mapsView!)
    let safeArea = view.safeAreaLayoutGuide
    NSLayoutConstraint.activate([
        mapsView!.topAnchor.constraint(equalTo: safeArea.topAnchor),mapsView!.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor),mapsView!.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor),mapsView!.trailingAnchor.constraint(equalTo: safeArea.leadingAnchor)
    ])
}

}

这是我的 Google 地图视图(或多或少直接从地图文档中复制)

class MapsViewController: UIView {

private let zoomLevel: Float = 10.0
private let fakeloc: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 59.9434781,longitude: 10.7918909)
private var mapView: GMSMapView!
var locationManager: CLLocationManager = CLLocationManager()
var currentLocation: CLLocation?
var preciseLocationZoomLevel: Float = 15.0
var approximateLocationZoomLevel: Float = 10.0

override init(frame: CGRect) {
    super.init(frame: frame)
    initGoogleMaps()
}



required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

private func initGoogleMaps(){
    initLocationManager()
    let defaultLocation = CLLocation(latitude: -33.869405,longitude: 151.199)

    let zoomLevel = locationManager.accuracyAuthorization == .fullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel
    let camera = GMSCameraPosition.camera(withLatitude: defaultLocation.coordinate.latitude,longitude: defaultLocation.coordinate.longitude,zoom: zoomLevel)
    mapView = GMSMapView.map(withFrame: self.bounds,camera: camera)
    mapView.settings.myLocationButton = true
    mapView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    mapView.isMyLocationEnabled = true
    self.addSubview(mapView)
    // Add the map to the view,hide it until we've got a location update.
    mapView.isHidden = false
}

private func initLocationManager(){
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.distanceFilter = 50
    locationManager.startUpdatingLocation()
    locationManager.delegate = self
}
}

extension MapsViewController: CLLocationManagerDelegate {


func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
    let location: CLLocation = locations.last!
    print("Location: \(location)")

    let zoomLevel = locationManager.accuracyAuthorization == .fullAccuracy ? preciseLocationZoomLevel : approximateLocationZoomLevel
    let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,longitude: location.coordinate.longitude,zoom: zoomLevel)

    if mapView.isHidden {
      mapView.isHidden = false
      mapView.camera = camera
    } else {
      mapView.animate(to: camera)
    }
    locationManager.stopUpdatingLocation()

  }


func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
    // Check accuracy authorization
    let accuracy = manager.accuracyAuthorization
    switch accuracy {
    case .fullAccuracy:
        print("Location accuracy is precise.")
    case .reducedAccuracy:
        print("Location accuracy is not precise.")
    @unkNown default:
      fatalError()
    }

    // Handle authorization status
    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.")
    @unkNown default:
      fatalError()
    }
  }

func locationManager(_ manager: CLLocationManager,didFailWithError error: Error) {
    locationManager.stopUpdatingLocation()
    print("Error: \(error)")
  }
}

这是我的控制台输出

Location accuracy is precise.
Location status not determined.
2021-04-05 12:41:24.619769+0200 my_app[3412:471542] Metal GPU Frame Capture Enabled
2021-04-05 12:41:24.620390+0200 my_app[3412:471542] Metal API Validation Enabled
Location accuracy is precise.
Location status not determined.
2021-04-05 12:41:25.180997+0200 my_app[3412:471253] Unbalanced calls to begin/end appearance transitions for <my_app.ViewController: 0x106208540>.

解决方法

RestTemplate

不应该是safeArea.trailiingConstraint吗?也许这就是原因。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...