放大到用户位置后,在允许访问位置后不缩放初始加载

时间:2018-12-31 16:49:36

标签: ios swift core-location

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

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

1 个答案:

答案 0 :(得分:0)

您需要实现locationManager(_:didChangeAuthorization:),以便您在被授予授权时可以进行响应。