ios - 允许访问位置后缩放到用户位置不缩放初始加载
问题描述
当应用程序在使用应用程序/总是/从不时请求访问您的位置的权限时,初始加载时。一旦你选择了一个选项,缩放到用户的位置就不会缩放,但是如果我返回一个屏幕然后返回到地图,它会缩放到用户的位置。选择权限后如何使其缩放?
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:)
,以便在获得授权时做出响应。
推荐阅读
- plot - Julia 中的 3D 矢量图
- android - Ionic 应用程序以 API 级别 25 为目标,并且必须至少以 API 级别 26 为目标
- javascript - 如何找到 moment() 构造函数引发弃用错误
- codenameone - CN1 如何处理home指示灯(iPhone X等)
- go - 如何检查文件名太长错误
- wpf - WPF ComboBox 删除高亮效果而不覆盖整个样式
- sql-server - SonarQube 7.2.1 - 连接到 SQL Server(多个实例)
- python-2.7 - Tkinter 中 st.mainloop 中的意外令牌
- grafana - 如何从 influxdb/grafana 中的两个查询中计算值
- ruby - 回调后 AASM 中的收益项