use*_*266 11 formatting gps android coordinates
在android(java)中,当你使用函数getlatitude()等获得当前纬度和经度时,你得到十进制格式的坐标:
纬度:24.3454523经度:10.123450
我想把它变成度数和小数分钟看起来像这样:
纬度:40°42'51"N经度:74°00'21"W
din*_*rma 16
从小数转换到度数你可以做如下
String strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_DEGREES);
String strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_DEGREES);
Run Code Online (Sandbox Code Playgroud)
参考是android开发者网站.
编辑
我尝试了以下内容并获得输出:
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
Run Code Online (Sandbox Code Playgroud)
根据您的要求尝试不同的选项
正如已经提到的,需要一些字符串操作。我创建了以下帮助程序类,它将位置转换为 DMS 格式并允许指定秒的小数位:
\n\nimport android.location.Location;\nimport android.support.annotation.NonNull;\n\npublic class LocationConverter {\n\n public static String getLatitudeAsDMS(Location location, int decimalPlace){\n String strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS);\n strLatitude = replaceDelimiters(strLatitude, decimalPlace);\n strLatitude = strLatitude + " N";\n return strLatitude;\n }\n\n public static String getLongitudeAsDMS(Location location, int decimalPlace){\n String strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS);\n strLongitude = replaceDelimiters(strLongitude, decimalPlace);\n strLongitude = strLongitude + " W";\n return strLongitude;\n }\n\n @NonNull\n private static String replaceDelimiters(String str, int decimalPlace) {\n str = str.replaceFirst(":", "\xc2\xb0");\n str = str.replaceFirst(":", "\'");\n int pointIndex = str.indexOf(".");\n int endIndex = pointIndex + 1 + decimalPlace;\n if(endIndex < str.length()) {\n str = str.substring(0, endIndex);\n }\n str = str + "\\"";\n return str;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
这是 Kotlin 版本,改编自 Martin Weber 的回答。它还设置正确的半球;N、S、W 或 E
\n\nobject LocationConverter {\n\n fun latitudeAsDMS(latitude: Double, decimalPlace: Int): String {\n val direction = if (latitude > 0) "N" else "S"\n var strLatitude = Location.convert(latitude.absoluteValue, Location.FORMAT_SECONDS)\n strLatitude = replaceDelimiters(strLatitude, decimalPlace)\n strLatitude += " $direction"\n return strLatitude\n }\n\n fun longitudeAsDMS(longitude: Double, decimalPlace: Int): String {\n val direction = if (longitude > 0) "W" else "E"\n var strLongitude = Location.convert(longitude.absoluteValue, Location.FORMAT_SECONDS)\n strLongitude = replaceDelimiters(strLongitude, decimalPlace)\n strLongitude += " $direction"\n return strLongitude\n }\n\n private fun replaceDelimiters(str: String, decimalPlace: Int): String {\n var str = str\n str = str.replaceFirst(":".toRegex(), "\xc2\xb0")\n str = str.replaceFirst(":".toRegex(), "\'")\n val pointIndex = str.indexOf(".")\n val endIndex = pointIndex + 1 + decimalPlace\n if (endIndex < str.length) {\n str = str.substring(0, endIndex)\n }\n str += "\\""\n return str\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
应该是一些数学:
(int)37.33168 => 37
37.33168 % 1 = 0.33168
0.33168 * 60 = 19.905 => 19
19.905 % 1 = 0.905
0.905 * 60 => 54
Run Code Online (Sandbox Code Playgroud)
与-122相同(如果为负值,则添加360)
编辑:可能有一些我不知道的API。
引用来源: 如何从android中的纬度值中找到度?
| 归档时间: |
|
| 查看次数: |
27113 次 |
| 最近记录: |