Based on SO answers, I made code that converts DEG to DMS using FORMAT
example: 40 ° 42'51 "N 74 ° 00'21" W
Call example: getLocationAsDMS (location, 8) 8 is the correct 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; }
Jack the ripper
source share