Ios didUpdateLocations在Swift 3中不起作用
我正在将我的应用程序更新为Swift 3,无法让它打印出位置坐标 我的info.plist文件中有Privacy-Location(使用时的位置)、Privacy-Location(始终的位置)和Privacy-Location(位置)使用说明Ios didUpdateLocations在Swift 3中不起作用,ios,swift,xcode,swift3,core-location,Ios,Swift,Xcode,Swift3,Core Location,我正在将我的应用程序更新为Swift 3,无法让它打印出位置坐标 我的info.plist文件中有Privacy-Location(使用时的位置)、Privacy-Location(始终的位置)和Privacy-Location(位置)使用说明 @IBAction func activateTestModeButton(_ sender: AnyObject) { locationManager.startUpdatingLocation() print("Method cal
@IBAction func activateTestModeButton(_ sender: AnyObject) {
locationManager.startUpdatingLocation()
print("Method called")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations newLocation: CLLocation, fromLocation oldLocation: CLLocation){
loadUser()
loadCoreData()
print("Updating location")
let latitude:String = "\(newLocation.coordinate.latitude)"
let longitude:String = "\(newLocation.coordinate.longitude)"
print("Latitude = \(newLocation.coordinate.latitude)")
print("Longitude = \(newLocation.coordinate.longitude)") }
它打印“方法调用”,但不打印其他内容。我还提供了didFailWithError方法,我认为这是必需的,它没有打印任何内容:
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
print("Location manager failed with error = \(error)")
}
其他相关代码,人们会认为我有:
/* Asking the user if we can access their location */
func Authorization(){
if CLLocationManager.locationServicesEnabled()
{
switch CLLocationManager.authorizationStatus()
{
case .authorizedAlways:
print("Authorized")
case .authorizedWhenInUse:
print("Authorized when in use")
case .denied:
displayAlertWithTitle("Not determined", message: "Location services are not allowed for this app")
case .notDetermined:
print("Not Determined")
if let manager = self.locationManager
{
manager.requestWhenInUseAuthorization()
}
case .restricted:
displayAlertWithTitle("Restricted", message: "Location services are not allowed for this app")
}
}
else
{
print("Location services are not enabled")
}
}
/* Location Detection */
func permission(){
if CLLocationManager.authorizationStatus() == .notDetermined
{
locationManager?.requestAlwaysAuthorization()
}
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
Authorization()
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager?.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager?.distanceFilter = 200
self.locationManager?.delegate = self
}
我建议您将管理位置的代码移动到类似这样的类中。这段代码来自我几个月前制作的一个应用程序,我测试了它,看它是否有效,我确实有效。所以试试看。您只需要创建这个类的一个对象。我在方法
locationManager(:didupdate)
中留下了一个带注释的打印,您可以取消注释以查看位置中的所有更改
我知道这并不完美,但它让我完成了任务
import Foundation
import MapKit
import CoreLocation
class Location: CLLocationManager, CLLocationManagerDelegate {
private var latitude:Double
private var longitud:Double
private let locationManager = CLLocationManager()
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
override init() {
self.latitude = 0.0
self.longitud = 0.0
self.locationManager.requestWhenInUseAuthorization()
super.init()
if CLLocationManager.locationServicesEnabled() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = (manager.location?.coordinate)!
//print("locations = \(locValue.latitude) \(locValue.longitude)")
self.latitude = locValue.latitude
self.longitud = locValue.longitude
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLatitude()->NSNumber{
return self.latitude as NSNumber
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLongitude()->NSNumber{
return self.longitud as NSNumber
}
}
我建议您将管理位置的代码移动到类似这样的类中。这段代码来自我几个月前制作的一个应用程序,我测试了它,看它是否有效,我确实有效。所以试试看。您只需要创建这个类的一个对象。我在方法
locationManager(:didupdate)
中留下了一个带注释的打印,您可以取消注释以查看位置中的所有更改
我知道这并不完美,但它让我完成了任务
import Foundation
import MapKit
import CoreLocation
class Location: CLLocationManager, CLLocationManagerDelegate {
private var latitude:Double
private var longitud:Double
private let locationManager = CLLocationManager()
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
override init() {
self.latitude = 0.0
self.longitud = 0.0
self.locationManager.requestWhenInUseAuthorization()
super.init()
if CLLocationManager.locationServicesEnabled() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = (manager.location?.coordinate)!
//print("locations = \(locValue.latitude) \(locValue.longitude)")
self.latitude = locValue.latitude
self.longitud = locValue.longitude
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLatitude()->NSNumber{
return self.latitude as NSNumber
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
func getLongitude()->NSNumber{
return self.longitud as NSNumber
}
}
您需要绝对确定调用
startupdatatinglocation()
的CLLocationManager
实例不是nil
同样奇怪的是,您在
viewDidLoad
中共享的代码将locationManager
初始化为Optional
属性,但是当您调用startUpdatingLocation()
时,它不再是可选属性了?如果是这样的话,iAction
是否连接到其他ViewController中?或者您还没有共享该IBOutlet
中显式打开可选
的所有代码?或者这可能是一个输入错误?您需要绝对确定调用startUpdatingLocation()
的CLLocationManager
实例不是nil
同样奇怪的是,您在
viewDidLoad
中共享的代码将locationManager
初始化为Optional
属性,但是当您调用startUpdatingLocation()
时,它不再是可选属性了?如果是这样的话,iAction
是否连接到其他ViewController中?或者您还没有共享该IBOutlet
中显式打开可选
的所有代码?或者这可能是一个输入错误?是locationManager
当您尝试调用startUpdatingLocation()
时,它是nil
?另外,您在调用locationManager的地方共享的代码。startUpdatingLocation()
意味着它永远不会是nil
,但是viewDidLoad
中的代码意味着locationManager
是可选的。也许您的问题有输入错误?当您尝试调用startUpdatingLocation()
时,是否为locationManager
nil
?此外,您在调用locationManager的地方共享的代码。startUpdatingLocation()
意味着它永远不会是nil
,但是viewDidLoad
中的代码意味着locationManager
是可选的。也许你的问题有错?