无法在罗盘Swift iOS中指向正确的位置

问题描述

我正在开发一种可以从当前位置指向其他位置的指南针,但是在实施该指南针后,我注意到每次选择新的目标位置进行导航或指向时,它总是指向同一方向。

>
  func locationManager(_ manager: CLLocationManager,didUpdateheading newheading: CLheading) {
    let heading = newheading.magneticheading
    let headingR = Measurement(value: newheading.trueheading,unit: UnitAngle.degrees).converted(to: .radians).value

    // To kNow if the Data that our device is correct,The "horizontalAccuracy" value should be grater then ZERO for valid GPS coordinate Data
    if heading < 0 {return}
    // Get the heading(direction)
    if(heading == -1){
        print("no location")
    }
    self.directionAngel = Double(self.getbearingBetweenTwoPoints1(latDestination: self.desiredLatit,lonDestination: self.desiredLongit))

    let north = -1 * heading.degreesToradians
    UIView.animate(withDuration: 1) {
        self.imageView.transform = CGAffineTransform(rotationAngle: CGFloat(north))
        self.directionArrowView.transform = CGAffineTransform(rotationAngle: CGFloat(self.directionAngel.degreesToradians - headingR))
    }
    self.updateheading(heading: heading)
}


  private  func getbearingBetweenTwoPoints1(latDestination:Double,lonDestination:Double) -> Double {
      
    let lat1 = getMyLocation().latitude.degreesToradians
    let lon1 = getMyLocation().longitude.degreesToradians
    
    let lat2 = latDestination.degreesToradians
    let lon2 = lonDestination.degreesToradians
    
      let dLon = lon2 - lon1
      let y = sin(dLon) * cos(lat2)
      let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon)
      var radiansbearing = atan2(y,x)
      if(radiansbearing < 0.0){
          radiansbearing += 2 * Double.pi
      }
    return radiansbearing
  }
let locationManager: CLLocationManager = {
    $0.requestAlwaysAuthorization()
    $0.requestWhenInUseAuthorization()
    $0.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    $0.startUpdatingLocation()
    $0.startUpdatingheading()
    return $0
}(CLLocationManager())

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)