首次加载时,当应用程序使用应用程序/始终/从不请求权限访问您的位置时。一旦您选择一个选项,缩放到用户位置就不会缩放,但是如果我返回屏幕然后返回到地图,它将缩放到用户位置。选择权限后如何将其缩放?
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()
}
}
您需要实施,locationManager(_:didChangeAuthorization:)
以便您在被授予授权时可以做出回应。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句