如何从android中的纬度值找到度数?

发布于 2024-10-03 12:17:23 字数 117 浏览 5 评论 0原文

我有一个双精度值的纬度和经度值(37.33168900,-122.03073100)。

我想将其转换为度值,例如 (37 19' 54 ,48 11' 52 )。

有什么想法吗?提前致谢。

I have a latitude and longitude values as a double value(37.33168900, -122.03073100).

I want to convert it to the degree value like (37 19' 54 ,48 11' 52 ).

Any Idea? Thanx in advance.

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

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

发布评论

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

评论(7

楠木可依 2024-10-10 12:17:23

这将为您提供以弧度、分钟和秒为单位的字符串:

String strLongitude = location.convert(location.getLongitude(), location.FORMAT_SECONDS);
String strLatitude = location.convert(location.getLatitude(), location.FORMAT_SECONDS);

This will give you Strings in degrees, minutes and seconds of arc:

String strLongitude = location.convert(location.getLongitude(), location.FORMAT_SECONDS);
String strLatitude = location.convert(location.getLatitude(), location.FORMAT_SECONDS);

.

妄想挽回 2024-10-10 12:17:23

用这个

public static String getFormattedLocationInDegree(double latitude, double longitude) {
    try {
        int latSeconds = (int) Math.round(latitude * 3600);
        int latDegrees = latSeconds / 3600;
        latSeconds = Math.abs(latSeconds % 3600);
        int latMinutes = latSeconds / 60;
        latSeconds %= 60;

        int longSeconds = (int) Math.round(longitude * 3600);
        int longDegrees = longSeconds / 3600;
        longSeconds = Math.abs(longSeconds % 3600);
        int longMinutes = longSeconds / 60;
        longSeconds %= 60;
        String latDegree = latDegrees >= 0 ? "N" : "S";
        String lonDegrees = longDegrees >= 0 ? "E" : "W";

        return  Math.abs(latDegrees) + "°" + latMinutes + "'" + latSeconds
                + "\"" + latDegree +" "+ Math.abs(longDegrees) + "°" + longMinutes
                + "'" + longSeconds + "\"" + lonDegrees;
    } catch (Exception e) {
        return ""+ String.format("%8.5f", latitude) + "  "
                + String.format("%8.5f", longitude) ;
    }
}

use this

public static String getFormattedLocationInDegree(double latitude, double longitude) {
    try {
        int latSeconds = (int) Math.round(latitude * 3600);
        int latDegrees = latSeconds / 3600;
        latSeconds = Math.abs(latSeconds % 3600);
        int latMinutes = latSeconds / 60;
        latSeconds %= 60;

        int longSeconds = (int) Math.round(longitude * 3600);
        int longDegrees = longSeconds / 3600;
        longSeconds = Math.abs(longSeconds % 3600);
        int longMinutes = longSeconds / 60;
        longSeconds %= 60;
        String latDegree = latDegrees >= 0 ? "N" : "S";
        String lonDegrees = longDegrees >= 0 ? "E" : "W";

        return  Math.abs(latDegrees) + "°" + latMinutes + "'" + latSeconds
                + "\"" + latDegree +" "+ Math.abs(longDegrees) + "°" + longMinutes
                + "'" + longSeconds + "\"" + lonDegrees;
    } catch (Exception e) {
        return ""+ String.format("%8.5f", latitude) + "  "
                + String.format("%8.5f", longitude) ;
    }
}
随心而道 2024-10-10 12:17:23

应该是一些数学:

(int)37.33168                => 37
37.33168 % 1 = 0.33168       
0.33168 * 60 = 19,905        => 19
19.905 % 1 = 0.905
0.905 * 60                   => 54

与 -122 相同(如果为负值则添加 360)

编辑:
可能有一些我不知道的API。

Should be some math:

(int)37.33168                => 37
37.33168 % 1 = 0.33168       
0.33168 * 60 = 19,905        => 19
19.905 % 1 = 0.905
0.905 * 60                   => 54

same with -122 (add 360 if nagative value)

EDIT:
May be there is some API, which I don't know.

另类 2024-10-10 12:17:23

另请查看 Location 类文档,尤其是 Convert() 方法,因为它应该做你想要的事情。

Also take a look at the Location class documentation especially at the convert() method as it should do just what you want.

饮惑 2024-10-10 12:17:23

实现此目的的另一种方法是使用以下函数

private String convertCoordinate(double coordinate, boolean isLatitude){
    String[] pString = isLatitude ? new String[]{"N", "S"} :  new String[]{"E", "W"};
    int degree = (int) coordinate;
    int minute = (int) (Math.abs(coordinate) * 60) % 60;
    int second = (int) (Math.abs(coordinate) * 3600) % 60;

    int index = degree < 0 ? 1 : 0;
    degree = Math.abs(degree);

    return degree + pString[index] + " " + minute + "' " + second + "\"";
}

Another way you can achieve this is using the following function

private String convertCoordinate(double coordinate, boolean isLatitude){
    String[] pString = isLatitude ? new String[]{"N", "S"} :  new String[]{"E", "W"};
    int degree = (int) coordinate;
    int minute = (int) (Math.abs(coordinate) * 60) % 60;
    int second = (int) (Math.abs(coordinate) * 3600) % 60;

    int index = degree < 0 ? 1 : 0;
    degree = Math.abs(degree);

    return degree + pString[index] + " " + minute + "' " + second + "\"";
}
我是男神闪亮亮 2024-10-10 12:17:23

阿比的答案在我所在的位置非常有效,但在其他一些地方会产生错误,例如在西半球上“E”而不是“W”。 IMO 应该是:

String lonDegrees = longDegrees >= 0 ? "E" : "W";

而不是:

String lonDegrees = latDegrees >= 0 ? "E" : "W";

Abi's answer works very well in my location, but produces errors in some others, e.g. "E" instead of "W" on the Western Hemisphere. IMO it should be:

String lonDegrees = longDegrees >= 0 ? "E" : "W";

instead of:

String lonDegrees = latDegrees >= 0 ? "E" : "W";
伊面 2024-10-10 12:17:23

基于SO答案,我编写了一个代码,使用FORMAT将DEG转换为DMS

示例:40°42′51″ N 74°00′21″ W

示例调用:getLocationAsDMS(location,8)
8 是适合此格式的数字

public String getLocationAsDMS (Location location, int decimalPlace){
    String strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS);
    strLatitude = replaceDelimiters(strLatitude, decimalPlace);
    char latCardinal = location.getLongitude() >= 0 ? 'N' : 'S';
    strLatitude = strLatitude + " " + latCardinal;

    String strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS);
    strLongitude = replaceDelimiters(strLongitude, decimalPlace);
    char lonCardinal = location.getLongitude() >= 0 ? 'E' : 'W';
    strLongitude = strLongitude + " " + lonCardinal;

    return strLatitude + " " + strLongitude;
}

@NonNull
private String replaceDelimiters(String str, int decimalPlace) {
    str = str.replaceFirst(":", "°");
    str = str.replaceFirst(":", "'");
    int pointIndex = str.indexOf(".");
    int endIndex = pointIndex + 1 + decimalPlace;
    if(endIndex < str.length()) {
        str = str.substring(0, endIndex);
    }
    str = str + "\"";
    return str;
}

Based on SO answers i made a code which convert DEG to DMS with FORMAT

example : 40°42′51″ N 74°00′21″ W

example call : getLocationAsDMS(location,8)
8 is the right number for this format

public String getLocationAsDMS (Location location, int decimalPlace){
    String strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS);
    strLatitude = replaceDelimiters(strLatitude, decimalPlace);
    char latCardinal = location.getLongitude() >= 0 ? 'N' : 'S';
    strLatitude = strLatitude + " " + latCardinal;

    String strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS);
    strLongitude = replaceDelimiters(strLongitude, decimalPlace);
    char lonCardinal = location.getLongitude() >= 0 ? 'E' : 'W';
    strLongitude = strLongitude + " " + lonCardinal;

    return strLatitude + " " + strLongitude;
}

@NonNull
private String replaceDelimiters(String str, int decimalPlace) {
    str = str.replaceFirst(":", "°");
    str = str.replaceFirst(":", "'");
    int pointIndex = str.indexOf(".");
    int endIndex = pointIndex + 1 + decimalPlace;
    if(endIndex < str.length()) {
        str = str.substring(0, endIndex);
    }
    str = str + "\"";
    return str;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文