我想转换纬度40.7127837,经度-74.0059413 并为以下格式
北纬 40°42'46.0218" 西经 74°0'21.3876"
最好的方法是什么?
我尝试了 location.FORMAT_DEGREES、location.FORMAT_MINUTES 和 location.FORMAT_SECONDS 等方法,但我不确定如何将它们转换为正确的格式。谢谢。
strLongitude = location.convert(location.getLongitude(), location.FORMAT_DEGREES);
strLatitude = location.convert(location.getLatitude(), location.FORMAT_DEGREES);
最佳答案
您正在使用的 Location.convert()
方法提供了非常好的结果,并且得到了很好的实现和测试。您只需要格式化输出以满足您的需要:
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();
}
当您使用输入坐标调用此方法时:
String locationString = convert(40.7127837, -74.0059413);
您将收到此输出:
N 40°42'46.02132" W 74°0'21.38868"
关于java - Android如何将纬度经度转换成度数格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38547870/