java - 如何在 java/android 中漂亮地格式化 GPS 数据?

标签 java android gps location string-formatting

我正在获取 2 个 double 的 gps 数据 - 一个 double 用于经度,另一个 double 用于纬度。

我正在将它们格式化为 Strings as

Location.convert(mGps.getLatitude(), Location.FORMAT_MINUTES);

但是我希望它的格式与谷歌地图上的格式相同,比如 17(这是度数的符号)55(这是分钟的符号)23(这是秒的符号)。

我该怎么做?

最佳答案

在此Wikipedia link您有一个 Java 实现来执行该转换。

// Input a double latitude or longitude in the decimal format
// e.g. 87.728056
String decimalToDMS(double coord) {
    String output, degrees, minutes, seconds;

    // gets the modulus the coordinate divided by one (MOD1).
    // in other words gets all the numbers after the decimal point.
    // e.g. mod = 87.728056 % 1 == 0.728056
    //
    // next get the integer part of the coord. On other words the whole number part.
    // e.g. intPart = 87

    double mod = coord % 1;
    int intPart = (int)coord;

    //set degrees to the value of intPart
    //e.g. degrees = "87"

    degrees = String.valueOf(intPart);

    // next times the MOD1 of degrees by 60 so we can find the integer part for minutes.
    // get the MOD1 of the new coord to find the numbers after the decimal point.
    // e.g. coord = 0.728056 * 60 == 43.68336
    //      mod = 43.68336 % 1 == 0.68336
    //
    // next get the value of the integer part of the coord.
    // e.g. intPart = 43

    coord = mod * 60;
    mod = coord % 1;
    intPart = (int)coord;

    // set minutes to the value of intPart.
    // e.g. minutes = "43"
    minutes = String.valueOf(intPart);

    //do the same again for minutes
    //e.g. coord = 0.68336 * 60 == 41.0016
    //e.g. intPart = 41
    coord = mod * 60;
    intPart = (int)coord;

    // set seconds to the value of intPart.
    // e.g. seconds = "41"
    seconds = String.valueOf(intPart);

    // I used this format for android but you can change it 
    // to return in whatever format you like
    // e.g. output = "87/1,43/1,41/1"
    output = degrees + "/1," + minutes + "/1," + seconds + "/1";

    //Standard output of D°M′S″
    //output = degrees + "°" + minutes + "'" + seconds + "\"";

    return output;
}

   /*
    * Conversion DMS to decimal 
    *
    * Input: latitude or longitude in the DMS format ( example: N 43° 36' 15.894")
    * Return: latitude or longitude in decimal format   
    * hemisphereOUmeridien => {W,E,S,N}
    *
    */
    public double DMSToDecimal(String hemisphereOUmeridien,double degres,double minutes,double secondes)
    {
            double LatOrLon=0;
            double signe=1.0;

            if((hemisphereOUmeridien=="W")||(hemisphereOUmeridien=="S")) {signe=-1.0;}              
            LatOrLon = signe*(Math.floor(degres) + Math.floor(minutes)/60.0 + secondes/3600.0);

            return(LatOrLon);               
    }

关于java - 如何在 java/android 中漂亮地格式化 GPS 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15547329/

相关文章:

java - 我们如何读取或使用输出流的内容

java - checkstyle 不检查 XML 中的空格缩进

java - 流收集 toMap - 类型不匹配

安卓撰写 : Type is defined multiple times

java - 无法解决方法占位符 fragment 错误

java - Eclipse 上的命令行命令

4.0 设备上的 Android 菜单按钮

python - 将 pandas DataFrame 或经度和纬度列表转换为 python 中的 gpx 文件

java - 如何检查 GPS 是否已禁用 Android

android - 如何解决 GPS 在后台 android Pie 中自动关闭?