didUpdateLocations在Swift 3中不起作用

时间:2017-05-31 17:37:24

标签: ios swift xcode swift3 core-location

我正在将我的应用更新为Swift 3,无法打印出位置坐标。

我有隐私 - 使用中的位置,隐私 - 位置始终和隐私 - 我的info.plist文件中的位置使用说明。

{
 "query": "Hello!",
 "page_number": 1,
 "result_per_page": 10
}

Hello!, 1, 10

它打印“方法被调用”,但没有别的。我还包含了didFailWithError方法,我认为这是必需的,它不会打印任何东西:

@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)")      }

其他相关的代码,人们会认为我有:

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
    print("Location manager failed with error = \(error)")
}

2 个答案:

答案 0 :(得分:1)

我建议您将管理Location的代码移动到类似这样的类。这段代码来自我几个月前制作的应用程序,我测试它是否有效,我做了。所以试试吧。您只需要创建此类的对象。我在方法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
    }

}

答案 1 :(得分:1)

您需要绝对确定CLLocationManager实例,您拨打的电话startUpdatingLocation()不是nil

同样奇怪的是,您在viewDidLoad中共享的代码会将locationManager初始化为Optional属性,但是当您致电startUpdatingLocation()时,它不再是可选的?如果那是IBAction连接在其他ViewController中的权利吗?或者您还没有共享明确展开IBOutlet的{​​{1}}中的所有代码?或者这可能是一个错字?