使用磁航向获取地理方向

发布于 2024-11-16 17:18:14 字数 112 浏览 2 评论 0原文

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

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

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

How to calculate magnetic heading value to get geographical direction i.e., North, North-East, East, East-South, South, South-West, West, West-North?

As magnetic heading returns only value of degree and its required for me to show the geographical direction, on the basis of continuous updated magnetic heading.

How can I make it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┈┾☆殇 2024-11-23 17:18:14

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

        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";
        }

在我这边工作得很好。如有需要修改的地方,欢迎各位大师告知。

Below is the code that I have used to display continuously updating geographical direction.

        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";
        }

It is working fine at my end. Masters are welcome to let me know if any modification required.

哑剧 2024-11-23 17:18:14

尝试一下 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)
}

太完美了!

Try this example offered by Matt Neuburg on 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)
}

It is perfect!

岁吢 2024-11-23 17:18:14
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";
}
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";
}
梅窗月明清似水 2024-11-23 17:18:14

Swift (5.0) 版本接受了 @alloc_iNit 的答案并进行了测试。

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

Swift (5.0) version of accepted answer from @alloc_iNit and tested as well.

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)")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文