Java 如何在android中从纬度值中查找度?
我将纬度和经度值作为双精度值(37.33168900,-122.03073100) 我想把它转换成度值,比如(37 19'54,48 11'52) 有什么想法吗?提前准备好 应该是一些数学:Java 如何在android中从纬度值中查找度?,java,android,location,Java,Android,Location,我将纬度和经度值作为双精度值(37.33168900,-122.03073100) 我想把它转换成度值,比如(37 19'54,48 11'52) 有什么想法吗?提前准备好 应该是一些数学: (int)37.33168 => 37 37.33168 % 1 = 0.33168 0.33168 * 60 = 19,905 => 19 19.905 % 1 = 0.905 0.905 * 60
(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,我不知道。还要看一看类文档,特别是
convert()
方法,因为它应该做您想要的事情。这将为您提供以弧度、分和秒为单位的字符串:
String strLongitude = location.convert(location.getLongitude(), location.FORMAT_SECONDS);
String strLatitude = location.convert(location.getLatitude(), location.FORMAT_SECONDS);
.使用此
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) ;
}
}
Abi的答案在我所在的位置上非常有效,但在其他一些位置上会产生错误,例如在西半球是“e”而不是“W”。海事组织应:
String lonDegrees = longDegrees >= 0 ? "E" : "W";
而不是:
String lonDegrees = latDegrees >= 0 ? "E" : "W";
实现这一点的另一种方法是使用以下函数
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 + "\"";
}
专用字符串转换坐标(双坐标,布尔isLatitude){
字符串[]pString=isLatitude?新字符串[]{“N”,“S”}:新字符串[]{“E”,“W”};
int度=(int)坐标;
整数分钟=(整数)(数学绝对值(坐标)*60)%60;
整数秒=(整数)(数学绝对值(坐标)*3600)%60;
int指数=度<0?1:0;
学位=数学abs(学位);
返回度+pString[索引]+“”+分钟+“”+秒+“”;
}
基于SO答案,我制作了一个代码,将DEG转换成DMS格式
示例:40°42′51〃北纬74°00′21〃西
示例调用:getLocationAsDMS(位置,8)
8是此格式的正确数字
公共字符串getLocationAsDMS(位置位置,int-decimalPlace){
字符串strLatitude=Location.convert(Location.getLatitude(),Location.FORMAT_秒);
strLatitude=替换分隔符(strLatitude,decimalPlace);
char latCardinal=location.getLongitude()>=0?'N':'S';
strLatitude=strLatitude+“”+latCardinal;
字符串strlongitute=Location.convert(Location.getLongitude(),Location.FORMAT_SECONDS);
strlongite=替换分隔符(strlongite,decimalPlace);
char lonCardinal=location.getLongitude()>=0?'E':'W';
标准长度=标准长度+“”+基数;
返回strLatitude+“”+strLongitude;
}
@非空
专用字符串替换分隔符(字符串str,int-decimalPlace){
str=str.replaceFirst(“:”,“°”);
str=str.replaceFirst(“:”,“”);
int pointIndex=str.indexOf(“.”);
int-endIndex=pointIndex+1+decimalPlace;
如果(endIndex
只是想澄清一下,你拥有的(37.33168900,-122.03073100)是度。你想要的(37 19'54,48 11'52)是度分秒。谢谢你的数学解释。:)给出一个示例代码不会有什么坏处。
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;
}