代码之家  ›  专栏  ›  技术社区  ›  Praveen

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

  •  5
  • Praveen  · 技术社区  · 15 年前

    我有一个纬度和经度值作为一个双倍值(37.33168900,-122.03073100)。

    我想把它转换成度值,比如(3719'54,4811'52)。

    知道吗?先走一步。

    7 回复  |  直到 15 年前
        1
  •  7
  •   Christian Kuetbach    15 年前

    应该是数学题:

    (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相同(如果为nagative值,则添加360)

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

        2
  •  12
  •   NickT    15 年前

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

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

    .

        3
  •  8
  •   mbonness    10 年前

    用这个

    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) ;
        }
    }
    
        4
  •  2
  •   Octavian Helm    15 年前

    也可以看看 Location 类文档,尤其是 convert() 方法,因为它应该做你想做的。

        5
  •  1
  •   Piotr Miller    11 年前

    Abi的答案在我的位置上非常有效,但在其他一些地方会产生错误,例如西半球的“e”而不是“W”。国际海事组织应:

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

    而不是:

    String lonDegrees = latDegrees >= 0 ? "E" : "W";
    
        6
  •  1
  •   Chris Stillwell    10 年前

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

    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 + "\"";
    }
    
        7
  •  0
  •   Jakub S.    9 年前

    基于这些答案我做了一个代码 用格式将DEG转换为DMS

    示例:40°42°51°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;
    }