6

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.

Praveen
  • 90,477
  • 74
  • 177
  • 219
  • 1
    Just to clarify, what you have (37.33168900, -122.03073100) is Degrees. What you want (37 19' 54 ,48 11' 52) is Degrees Minutes Seconds. – CaseyB Nov 18 '10 at 14:23

7 Answers7

13

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);

.

NickT
  • 23,844
  • 11
  • 78
  • 121
8

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) ;
    }
}
mbonness
  • 1,612
  • 1
  • 18
  • 20
abi
  • 1,002
  • 12
  • 15
7

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.

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
2

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

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
1

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";
Piotr Miller
  • 311
  • 2
  • 12
1

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 + "\"";
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
1

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;
}
Community
  • 1
  • 1
Jakub S.
  • 5,580
  • 2
  • 42
  • 37