0
我想我的应用程序采取积极和背景模式的位置(如果仅使用NSLocationAlwaysUsageDescription的许可,则myLocationButton不显示)。 我在Info.plist中设置:只显示一个位置的权限警报
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>UIBackgroundModes</key>
而添加的MapViewController
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.requestAlwaysAuthorization()
但是,应用程序启动时,只显示第一位置的权限警报时,并且应用程序后出现的第二个警报的权限重新开放。
更新:
override func viewDidLoad() {
super.viewDidLoad()
...
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
viewMap.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
self.startLocationUpdates()
...
}
func startLocationUpdates() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.activityType = CLActivityType.automotiveNavigation
self.locationManager.distanceFilter = distanceFilterMetr
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let myLocation: CLLocation = change?[NSKeyValueChangeKey.newKey] as! CLLocation
viewMap.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: observeZoom)
mapRoute.coordinateLatitude = myLocation.coordinate.latitude
mapRoute.coordinateLongitude = myLocation.coordinate.longitude
viewMap.delegate = self
viewMap.settings.myLocationButton = true
viewMap.settings.compassButton = true
didFindMyLocation = true
}
您只需要请求“始终”授权。这使您的应用有权在前台和后台使用位置信息。 – Paulw11
@ Paulw11我写了如果只使用NSLocationAlwaysUsageDescription权限,然后myLocationButton不显示(位置更新但按钮不显示)iOS 10.3 swift 3 – Roman
你如何决定是否显示该按钮?我可以向你保证,总是身份验证是前台和后台位置所需要的。 – Paulw11