Search code examples
androidlatitude-longitude

I want to change the coordinates of dd format to dmm way in android


I want to convert latitude 41.40338, longitude 2.17403 and to the following format

41 24.2028, 2 10.4418

I want to convert dd to dmm format as above


Solution

  • 41.40338 and 2.17403 are degrees and decimals of degree.

    41 24.2028, 2 10.4418 are in degrees, minutes and decimals of minutes.

    So, you get the decimal part of 41.40338 which is 0.40338 and multiply it by 60: 0.40338 * 60 = 24.2028 and 0.17403 * 60 = 10.4418

    That numbers are the minutes and decimal minutes of the latitude and longitude.

    The code could be something like:

    private String transformCoord(double coord) {
        int intPart = (int) coord;
        double decimalPart = (coord - intPart);
        return "" + intPart + " " + (decimalPart * 60);
    }
    

    And you should call transformCoord with the latitude and the longitude as double to get the values you want as string

    String desiredCoord = transformCoord(41.40338) + ", " + transformCoord(2.17403);