【问题标题】:Get geographical direction using magnetic heading使用磁航向获取地理方向
【发布时间】:2011-09-20 02:55:04
【问题描述】:

如何计算磁航向值以获得地理方向,即北、东北、东、东、南、南、西南、西、西、北?

由于磁航向只返回度数值,我需要在不断更新磁航向的基础上显示地理方向。

我怎样才能使它成为可能?

【问题讨论】:

  • 我认为他们已经为 mapKit 库添加了功能。你应该检查一下。
  • 我没有找到这样的功能。如果万一,你发现了,请告诉我。
  • 我自己找出解决办法。我已经测试了实现的逻辑,并且还与 Compass 进行了比较,并且它在我的应用程序中运行良好。

标签: iphone objective-c compass-geolocation


【解决方案1】:

下面是我用来显示不断更新的地理方向的代码。

        CGFloat currentHeading = newHeading.magneticHeading;
        NSString *strDirection = [[NSString alloc] init];

        if(gradToRotate >23 && gradToRotate <= 67){
              strDirection = @"NE";
        } else if(gradToRotate >68 && gradToRotate <= 112){
              strDirection = @"E";
        } else if(gradToRotate >113 && gradToRotate <= 167){
              strDirection = @"SE";
        } else if(gradToRotate >168 && gradToRotate <= 202){
              strDirection = @"S";
        } else if(gradToRotate >203 && gradToRotate <= 247){
              strDirection = @"SW";
        } else if(gradToRotate >248 && gradToRotate <= 293){
              strDirection = @"W";
        } else if(gradToRotate >294 && gradToRotate <= 337){
              strDirection = @"NW";
        } else if(gradToRotate >=338 || gradToRotate <= 22){
              strDirection = @"N";
        }

我的工作正常。如果需要修改,欢迎各位高手告诉我。

【讨论】:

  • 我找不到标准度数来识别确切的地理方向,这就是为什么将此计算设置为与指南针比较。
  • 太棒了(即使在两年后对人们也有帮助):-).. 你是如何获得这些学位值的?有什么来源吗?
  • 谢谢。无源,纯自实现逻辑。
  • 真的很棒!也帮了我!度数 > 22 但 < 23 而不是 &lt;= 22。这样,如果某物略微 NE但基本上是N,它将返回"N"
【解决方案2】:

试试这个由 Matt Neuburg 在 Github 上提供的例子。 https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch22p775heading/ch35p1035heading/ViewController.swift

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    var h = newHeading.magneticHeading
    let h2 = newHeading.trueHeading // will be -1 if we have no location info
    print("\(h) \(h2) ")
    if h2 >= 0 {
        h = h2
    }
    let cards = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]
    var dir = "N"
    for (ix, card) in cards.enumerate() {
        if h < 45.0/2.0 + 45.0*Double(ix) {
            dir = card
            break
        }
    }
    print(dir)
}

太完美了!

【讨论】:

    【解决方案3】:
    CLLocationDirection  heading = ((newHeading.trueHeading > 0) ?
                                       newHeading.trueHeading : newHeading.magneticHeading);
    
    NSString *geoDirectionString = [[NSString alloc] init];
    
    if(heading >22.5 && heading <= 67.5){
        geoDirectionString = @"North East";
    } else if(heading >67.5 && heading <= 112.5){
        geoDirectionString = @"East";
    } else if(heading >112.5 && heading <= 157.5){
        geoDirectionString = @"South East";
    } else if(heading >157.5 && heading <= 202.5){
        geoDirectionString = @"South";
    } else if(heading >202.5 && heading <= 247.5){
        geoDirectionString = @"South West";
    } else if(heading >248 && heading <= 293){
        geoDirectionString = @"West";
    } else if(heading >247.5 && heading <= 337.5){
        geoDirectionString = @"North West";
    } else if(heading >=337.5 || heading <= 22.5){
        geoDirectionString = @"North";
    }
    

    【讨论】:

      【解决方案4】:

      @alloc_iNit 接受的答案的 Swift (5.0) 版本并经过测试。

      private func updateDirectionTextFromHeading(_ magneticHeading:Double) {
          let gradToRotate = Int(magneticHeading)
          var strDirection = ""
          
          if(gradToRotate > 23 && gradToRotate <= 67){
              strDirection = "North East";
          } else if(gradToRotate > 68 && gradToRotate <= 112){
              strDirection = "East";
          } else if(gradToRotate > 113 && gradToRotate <= 167){
              strDirection = "Soth East";
          } else if(gradToRotate > 168 && gradToRotate <= 202){
              strDirection = "South";
          } else if(gradToRotate > 203 && gradToRotate <= 247){
              strDirection = "South West";
          } else if(gradToRotate > 248 && gradToRotate <= 293){
              strDirection = "West";
          } else if(gradToRotate > 294 && gradToRotate <= 337){
              strDirection = "North West"
          } else if(gradToRotate >= 338 || gradToRotate <= 22){
              strDirection = "North"
          }
          
          print("\(gradToRotate)° \(strDirection)")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-03
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        相关资源
        最近更新 更多