使用CLLocation的两点之间的距离会有所不同,尽管设备没有移动

时间:2018-03-31 14:56:42

标签: ios swift cocoa-touch core-location cllocation

我读取QR码的纬度和经度,因为这些信息在QR码内,如下所示:

class ScannedQRCode {
    let issuer : String
    let eventName : String
    let eventCode : String
    let issuedYear : Int
    let issuedMonth : Int
    let issuedDay : Int
    let issuedHour : Int
    let issuedMin : Int
    let locatoin : CLLocation



    init(issuer : String , eventName : String , eventCode : String , issuedYear : Int ,issuedMonth : Int ,  issuedDay : Int , issuedHour : Int , issuedMin : Int , lat : Double , long : Double) {
        self.issuer = issuer
        self.eventName = eventName
        self.eventCode = eventCode
        self.issuedYear = issuedYear
        self.issuedMonth = issuedMonth
        self.issuedDay = issuedDay
        self.issuedHour = issuedHour
        self.issuedMin = issuedMin
        self.locatoin = CLLocation(latitude: lat, longitude: long)
    }



    public func getIssuer () -> String {return self.issuer}
    public func getEventName () -> String {return self.eventName}
    public func getEventCode () -> String {return self.eventCode}
    public func getIssuedYear () -> Int {return self.issuedYear}
    public func getIssuedMonth () -> Int {return self.issuedMonth}
    public func getIssuedDay () -> Int {return self.issuedDay}
    public func getIssuedHour () -> Int {return self.issuedHour}
    public func getIssuedMin () -> Int {return self.issuedMin}
    public func getLocatoin () -> CLLocation {return self.locatoin}




    public func getDescription () -> String {
        return ("The given QR code is generated by \(self.getIssuer()) on the year  \(self.getIssuedYear()) and month \(self.getIssuedMonth()) and day \(self.getIssuedDay()) and hour \(self.getIssuedHour()) and minutes \(self.getIssuedMin()) and for an event called \(self.getEventName()) and having the code \(self.getEventCode()) for the following location lat: \(self.getLocatoin().coordinate.latitude) and Long : \(self.getLocatoin().coordinate.longitude)")

    }


    func validateLocatoin () -> Bool{
        var validation = false


        let Mylocation = CLLocation(latitude: myLocation().latitude, longitude: myLocation().longitude)
        let distance = calculateDistance(LocationA: self.getLocatoin() , LocationB: Mylocation )
        print("The distance is : \(distance)")
        if (distance < 20 ) {

            validation = true
        }


        return validation
    }




    func validateTime () -> Bool {
        var validation = false


        let date = Date()
        let calendar = Calendar.current
        let components = calendar.dateComponents([.year, .month, .day, .hour ,.minute], from: date)

         let year =  (components.year)!
         let month = (components.month)!
         let day =  (components.day)!
         let hour = (components.hour)!
         let min =  (components.minute)!




        if (self.getIssuedYear() == year  && self.getIssuedMonth() == month && self.getIssuedDay() == day && self.getIssuedHour() == hour ){
            if (min - self.getIssuedMin() < 15){
                    validation = true
            }
        }


        return validation
    }

}

我还创建了另一个名为myLocation的函数,如下所示:

public func myLocation () -> CLLocationCoordinate2D {
    var currentLocation: CLLocation!
    var locManager = CLLocationManager()
    locManager.requestWhenInUseAuthorization()

    if ( CLLocationManager.authorizationStatus() == .authorizedAlways || CLLocationManager.authorizationStatus() == .authorizedWhenInUse )  {
    currentLocation = locManager.location

    }else {
        myLocation()
    }

    return currentLocation.coordinate

}

这是我的计算两点距离的代码:

public func calculateDistance (LocationA : CLLocation , LocationB : CLLocation) -> Double{
    let distanceInMeters = LocationA.distance(from: LocationB)
    return distanceInMeters
}

我称之为:

let attendance = ScannedQRCode(issuer: String(scannedCode[0]),
                                       eventName:String(scannedCode[1]) ,
                                       eventCode: String(scannedCode[2]),
                                       issuedYear: Int(scannedCode[3])!,
                                       issuedMonth: Int(scannedCode[4])!,
                                       issuedDay: Int(scannedCode[5])!,
                                       issuedHour: Int(scannedCode[6])!,
                                       issuedMin: Int(scannedCode[7])!,
                                       lat: Double(scannedCode[8])!,
                                       long: Double(scannedCode[9])!
        )

然后当我调用validateLocation时,它给了我不同的结果,即使我根本不动。知道我在哪里弄错了吗?

当我打印距离时,我得到了The distance is : 3.63338273593823e-08而我只是移动了我的手。根据堆栈溢出的其他问题,这个值是以米为单位测量的,它没有意义吗?

0 个答案:

没有答案