Ios 位置服务正常,但Lat&;长时间不装货
我已经在正在使用的iOS应用程序中成功地设置了位置服务(用户可以在移动滑块后选择允许位置服务),但我的代码中没有一个尝试获取用户当前位置的代码(通过long/lat)起作用。可能需要一些帮助来找出可能的错误。。。谢谢Ios 位置服务正常,但Lat&;长时间不装货,ios,swift,core-location,cllocationmanager,Ios,Swift,Core Location,Cllocationmanager,我已经在正在使用的iOS应用程序中成功地设置了位置服务(用户可以在移动滑块后选择允许位置服务),但我的代码中没有一个尝试获取用户当前位置的代码(通过long/lat)起作用。可能需要一些帮助来找出可能的错误。。。谢谢 import UIKit import MapKit import CoreLocation class SignUpSettingsViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet
import UIKit
import MapKit
import CoreLocation
class SignUpSettingsViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var locationOn: UISwitch!
var locationManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func locationOn(_ sender: UISwitch) {
if (sender.isOn == true){
print("Location SLIDER TURNED ON")
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
locationManager?.startUpdatingLocation()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
func locationManager2(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
print(location.coordinate.latitude)
print(location.coordinate.longitude)
}
}
}
else {
print ("Location SLIDER NOT ON")
}
}
再看看。
您在@IBAction func locationOn(uu-sender:UISwitch)中定义了func locationManager(u-manager:CLLocationManager,didUpdateLocations locations:[CLLocation])
将其移到类SignupSettings ViewController中,因为您在CLLocationManager代理中犯了错误。UIViewController或其他类应通过扩展以协议形式实现委托,或在类声明后添加CLLocationManagerDelegate。尝试将文件代码替换为:
import UIKit
import MapKit
import CoreLocation
// MARK: - Location manager delegate
extension SignUpSettingsViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways: break
case .authorizedWhenInUse: break
case .denied, .restricted: break /// location permission denied, or updated in Settings
case .notDetermined: break /// location permission wasn't selected
@unknown default: break
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
print(location)
}
}
// MARK: - SignUpSettingsViewController
final class SignUpSettingsViewController: UIViewController {
@IBOutlet weak var locationOn: UISwitch!
private let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func locationOn(_ sender: UISwitch) {
if (sender.isOn == true) {
print("Location SLIDER TURNED ON")
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.delegate = self
} else {
locationManager.delegate = nil
print ("Location SLIDER NOT ON")
}
}
}
在此函数中,您应该处理您的位置权限
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways: break
case .authorizedWhenInUse: break
case .denied, .restricted: break /// location permission denied, or updated in Settings
case .notDetermined: break /// location permission wasn't selected
@unknown default: break
}
}
只需重新检查info.plist文件中需要添加的位置键
另一个步骤是你需要保持
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
方法超出UISwitch操作方法范围。
我通过上述更改运行您的相同代码,它对我有效
请检查以下代码以供参考:
var locationManager: CLLocationManager?
@IBOutlet weak var locationOn: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func locationOn(_ sender: UISwitch) {
if (sender.isOn == true){
print("Location SLIDER TURNED ON")
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()
locationManager?.startUpdatingLocation()
// func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
// print("locations = \(locValue.latitude) \(locValue.longitude)")
// }
// func locationManager2(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// if let location = locations.last {
// print(location.coordinate.latitude)
// print(location.coordinate.longitude)
// }
// }
}
else {
print ("Location SLIDER NOT ON")
}
}
//MARK: Location service Delegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}
祝你好运:)