繁体   English   中英

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

[英]Location prompt disappears after showing inside 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)
    ])
}

}

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

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)")
  }
}

这是来自控制台的我的 output

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>.
    mapsView!.trailingAnchor.constraint(equalTo: safeArea.leadingAnchor)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM