How to find degree from the latitude value in android?

随声附和 提交于 2019-12-12 08:09:37

问题


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.


回答1:


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.




回答2:


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

.




回答3:


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



回答4:


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




回答5:


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



回答6:


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 + "\"";
}



回答7:


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


来源:https://stackoverflow.com/questions/4214969/how-to-find-degree-from-the-latitude-value-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!