允许访问位置后缩放到用户位置不缩放初始加载
zoom to user location not zooming on initial loading after allowing access to to location
在初始加载时,当应用程序在使用 app/always/never 时请求访问您的位置的权限。一旦你 select 一个选项,缩放到用户位置不会缩放,但如果我返回屏幕然后返回地图,它会缩放到用户位置。一旦获得 select 许可,我怎样才能让它缩放?
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:)
以便在您获得授权时做出响应。
在初始加载时,当应用程序在使用 app/always/never 时请求访问您的位置的权限。一旦你 select 一个选项,缩放到用户位置不会缩放,但如果我返回屏幕然后返回地图,它会缩放到用户位置。一旦获得 select 许可,我怎样才能让它缩放?
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:)
以便在您获得授权时做出响应。