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.
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.
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);
.
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) ;
}
}
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.
Also take a look at the Location class documentation especially at the convert()
method as it should do just what you want.
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";
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 + "\"";
}
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;
}