Swift 视线核心定位或核心运动

Swift 视线核心定位或核心运动,swift,core-location,core-motion,Swift,Core Location,Core Motion,获得视线(iPhone的方向)的最佳方法是什么 我已经用CoreLocation实现了一个: func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { self.currentDegreesLOS = newHeading.magneticHeading if (newHeading.headingAccuracy > 0) {

获得视线(iPhone的方向)的最佳方法是什么

我已经用CoreLocation实现了一个:

   func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    self.currentDegreesLOS = newHeading.magneticHeading

    if (newHeading.headingAccuracy > 0) { 
        if self.currentDegreesLOS < 135.0 && self.currentDegreesLOS > 45.0 {

                self.currentDegreesLOS = 90.0
            }
                //South direction
            else if self.currentDegreesLOS < 45.0 ||  self.currentDegreesLOS > 315.0 {

                self.currentDegreesLOS = 360.0
            }
                //West direction
            else if self.currentDegreesLOS > 225.0 && self.currentDegreesLOS < 315.0 {

                self.currentDegreesLOS = 270.0
            }
                //North direction
            else if self.currentDegreesLOS > 135.0 &&  self.currentDegreesLOS < 225.0{

                self.currentDegreesLOS = 180.0
            }
    }
  }
func locationManager(管理器:CLLocationManager,didUpdateHeading新标题:CLHeading){
self.currentDegreesLOS=newHeading.magneticeheading
如果(newHeading.headingAccuracy>0){
如果self.currentDegreesLOS<135.0&&self.currentDegreesLOS>45.0{
自电流度LOS=90.0
}
//南向
否则,如果self.currentDegreesLOS<45.0 | | self.currentDegreesLOS>315.0{
self.currentDegreesLOS=360.0
}
//西向
否则,如果self.currentDegreesLOS>225.0和self.currentDegreesLOS<315.0{
自电流度LOS=270.0
}
//北向
否则,如果self.currentDegreesLOS>135.0和self.currentDegreesLOS<225.0{
自电流度LOS=180.0
}
}
}
但在这一点上,我第一次尝试将设备保持在北向360度。如果我在第二次运行时进行测试,同一方向将得到180°或270°

我尝试的另一个解决方案是CoreMotion,但我遇到了同样的问题:

        motionManager.startDeviceMotionUpdatesToQueue(queue, withHandler: { (data, error) -> Void in

    self.quat = data?.attitude.quaternion

        //Possibility 1
        var attitude:CMAttitude = (data?.attitude)!
        var rm:CMRotationMatrix = attitude.rotationMatrix

        // Get the heading.
        var heading = M_PI + atan2(rm.m22, rm.m12)

        heading = heading*180/M_PI
        if( heading < 0 ){
            heading += 360.0;// convert negative to positive angles

        }
        })
motionManager.startDeviceMotionUpdatesToQueue(队列,withHandler:{(数据,错误)->中的Void
self.quat=数据?.station.quaternion
//可能性1
var姿态:Cmatitude=(数据?.patitude)!
变量rm:CMRotationMatrix=姿态旋转矩阵
//记下标题。
可变航向=M_PI+atan2(m22 rm,m12 rm)
航向=航向*180/M\u PI
如果(标题<0){
航向+=360.0;//将负角度转换为正角度
}
})

有人知道我如何使用上述方法之一获得最佳结果吗?

那么,在第一个示例中,您是否真正注意到了
磁读取的原始值?或者您只是在通过附加逻辑运行之后查看了这些值吗?你看过这类东西的核心位置文档了吗?我看过这两种价值观。原始值也会受到相同问题的影响。在文档中我没有发现任何关于这方面的信息。你得到了什么样的原始值?您希望得到什么样的原始值?关于磁航向,文档中到底说了些什么?根据文档,原始值是否与您期望的值有关?若否,原因为何?这个问题需要更多的努力,等等。这个问题的当前形式是“我不知道我在做什么。为我写我的代码。”例如,你有没有在搜索短语
“corelocation magnetic heading”
时读过哪个是谷歌搜索结果的第一名,而且,你有没有校准过设备?磁航向用….计算。。。。磁学。。。。除了地球的磁层(或使罗盘指向北方的任何东西),它还受到磁性物质的严重影响。对于校准,我添加了校准的代理方法:LocationManager应显示HeadingCalibration(manager:CLLocationManager)。但是这个方法不会被调用!因此,根据文件,航向精度是正常的!