The Location.convert()
method that you are using gives very good results and is well implemented and tested. You just need to format the output to suit your needs:
private String convert(double latitude, double longitude) {
StringBuilder builder = new StringBuilder();
if (latitude < 0) {
builder.append("S ");
} else {
builder.append("N ");
}
String latitudeDegrees = Location.convert(Math.abs(latitude), Location.FORMAT_SECONDS);
String[] latitudeSplit = latitudeDegrees.split(":");
builder.append(latitudeSplit[0]);
builder.append("°");
builder.append(latitudeSplit[1]);
builder.append("'");
builder.append(latitudeSplit[2]);
builder.append("\"");
builder.append(" ");
if (longitude < 0) {
builder.append("W ");
} else {
builder.append("E ");
}
String longitudeDegrees = Location.convert(Math.abs(longitude), Location.FORMAT_SECONDS);
String[] longitudeSplit = longitudeDegrees.split(":");
builder.append(longitudeSplit[0]);
builder.append("°");
builder.append(longitudeSplit[1]);
builder.append("'");
builder.append(longitudeSplit[2]);
builder.append("\"");
return builder.toString();
}
When you call this method with your input coordinates:
String locationString = convert(40.7127837, -74.0059413);
You will receive this output:
N 40°42'46.02132" W 74°0'21.38868"