在android(java)中,当你使用函数getlatitude()等获得当前纬度和经度时,你得到十进制格式的坐标:
纬度:24.3454523经度:10.123450
我想把它变成度数和小数分钟看起来像这样:
纬度:40°42'51“N经度:74°00'21”W
答案 0 :(得分:16)
从小数转换为度数,您可以按照以下步骤
String strLongitude = location.convert(location.getLongitude(), location.FORMAT_DEGREES);
String strLatitude = location.convert(location.getLatitude(), location.FORMAT_DEGREES);
修改强>
我尝试了以下内容并获得输出:
strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_DEGREES);
strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_DEGREES);
OUTPUT : Long: 73.16584: Lat: 22.29924
strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS);
strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS);
OUTPUT : Long: 73:9:57.03876: Lat: 22:17:57.26472
strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_MINUTES);
strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_MINUTES);
OUTPUT : Long: 73:9.95065: Lat: 22:17.95441
根据您的要求尝试不同的选项
答案 1 :(得分:4)
应该是一些数学:
(int)37.33168 => 37
37.33168 % 1 = 0.33168
0.33168 * 60 = 19.905 => 19
19.905 % 1 = 0.905
0.905 * 60 => 54
与-122相同(如果为负值则添加360)
编辑:可能有一些API,我不知道。
请参阅: How to find degree from the latitude value in android?
Convert Latitude and Longitude Values (Degrees) to Double . Java
答案 2 :(得分:4)
如前所述,需要进行一些字符串操作。我创建了以下帮助器类,它将位置转换为DMS格式,并允许指定秒的小数位:
import android.location.Location;
import android.support.annotation.NonNull;
public class LocationConverter {
public static String getLatitudeAsDMS(Location location, int decimalPlace){
String strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS);
strLatitude = replaceDelimiters(strLatitude, decimalPlace);
strLatitude = strLatitude + " N";
return strLatitude;
}
public static String getLongitudeAsDMS(Location location, int decimalPlace){
String strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS);
strLongitude = replaceDelimiters(strLongitude, decimalPlace);
strLongitude = strLongitude + " W";
return strLongitude;
}
@NonNull
private static 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;
}
}
答案 3 :(得分:1)
使用此
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 :(得分:1)
这是一个Kotlin版本,改编自Martin Weber的回答。它还设置了正确的半球; N,S,W或E
object LocationConverter {
fun latitudeAsDMS(latitude: Double, decimalPlace: Int): String {
val direction = if (latitude > 0) "N" else "S"
var strLatitude = Location.convert(latitude.absoluteValue, Location.FORMAT_SECONDS)
strLatitude = replaceDelimiters(strLatitude, decimalPlace)
strLatitude += " $direction"
return strLatitude
}
fun longitudeAsDMS(longitude: Double, decimalPlace: Int): String {
val direction = if (longitude > 0) "W" else "E"
var strLongitude = Location.convert(longitude.absoluteValue, Location.FORMAT_SECONDS)
strLongitude = replaceDelimiters(strLongitude, decimalPlace)
strLongitude += " $direction"
return strLongitude
}
private fun replaceDelimiters(str: String, decimalPlace: Int): String {
var str = str
str = str.replaceFirst(":".toRegex(), "°")
str = str.replaceFirst(":".toRegex(), "'")
val pointIndex = str.indexOf(".")
val endIndex = pointIndex + 1 + decimalPlace
if (endIndex < str.length) {
str = str.substring(0, endIndex)
}
str += "\""
return str
}
}
答案 5 :(得分:1)
我想要类似的东西,在这里和维基百科研究答案之后,发现有一种格式化坐标的标准,ISO 6709#Annex D描述了坐标所需的文本表示形式,考虑到这一点并希望在其中具有可移植且紧凑的实现Kotlin,我以这段代码结束了,希望对其他人有用,
import kotlin.math.abs
import java.util.Locale
// https://en.wikipedia.org/wiki/ISO_6709#Representation_at_the_human_interface_(Annex_D)
fun formatCoordinateISO6709(lat: Double, long: Double, alt: Double? = null) = listOf(
abs(lat) to if (lat >= 0) "N" else "S", abs(long) to if (long >= 0) "E" else "W"
).joinToString(" ") {
val degrees = it.first.toInt()
val minutes = ((it.first - degrees) * 60).toInt()
val seconds = ((it.first - degrees) * 3600 % 60).toInt()
"%d°%02d′%02d″%s".format(Locale.US, degrees, minutes, seconds, it.second)
} + (alt?.let { " %s%.1fm".format(Locale.US, if (alt < 0) "−" else "", abs(alt)) } ?: "")
答案 6 :(得分:0)
你有一个十进制度的坐标,这种表示格式叫做“DEG”
你想要一个DEG到DMS(度,分,秒)(例如40°42'51“N),
转换。
http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
上的这个java代码实现如果DEG坐标值是&lt; 0,经度为西,纬度为南。
答案 7 :(得分:0)
使用这个方法并将坐标传递给方法
private String convertDegMinsSecs(double latitude, double longitude) {
StringBuilder builder = new StringBuilder();
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("\"");
if (latitude < 0) {
builder.append("S ");
} else {
builder.append("N ");
}
builder.append(" ");
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("\"");
if (longitude < 0) {
builder.append("W ");
} else {
builder.append("E ");
}
return builder.toString();
}