在初次加载时,当应用程序在使用应用程序时始终/永远/从不要求访问权限。一旦您选择一个选项,缩放到用户位置就不会缩放,但是如果我返回屏幕然后返回到地图,它将缩放到用户位置。选择权限后如何将其缩放?
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()
}
}
答案 0 :(得分:0)
您需要实现locationManager(_:didChangeAuthorization:)
,以便您在被授予授权时可以进行响应。