首页 > 解决方案 > 允许访问位置后缩放到用户位置不缩放初始加载

问题描述

当应用程序在使用应用程序/总是/从不时请求访问您的位置的权限时,初始加载时。一旦你选择了一个选项,缩放到用户的位置就不会缩放,但是如果我返回一个屏幕然后返回到地图,它会缩放到用户的位置。选择权限后如何使其缩放?

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    // Check for Location Services
    if (CLLocationManager.locationServicesEnabled()) {
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
    }

    //Zoom to user location
    if let userLocation = locationManager.location?.coordinate {
        let viewRegion = MKCoordinateRegion.init(center: userLocation, latitudinalMeters: 10000, longitudinalMeters: 10000)
        map.setRegion(viewRegion, animated: true)
    }

    DispatchQueue.main.async {
        self.locationManager.startUpdatingLocation()
    }

    //This will pull the info for where to put annotation
    let annotatedPlace = MKPointAnnotation()
    annotatedPlace.title = annoTitle
    annotatedPlace.coordinate = CLLocationCoordinate2D(latitude: coordinanteLat, longitude: coordinanteLong)
    map.addAnnotation(annotatedPlace)

    locationAuthStatus()

}

func locationAuthStatus() {
    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
        map.showsUserLocation = true
    } else {
        locationManager.requestWhenInUseAuthorization()
    }
}

标签: iosswiftcore-location

解决方案


您需要实施locationManager(_:didChangeAuthorization:),以便在获得授权时做出响应。


推荐阅读