Android 如何格式化GPS纬度和经度?

Android 如何格式化GPS纬度和经度?,android,gps,formatting,coordinates,Android,Gps,Formatting,Coordinates,在android(java)中,当使用函数getlatitude()等获取当前纬度和经度时,将以十进制格式获取坐标: 纬度:24.3454523经度:10.123450 我想把它转换成度数和十进制分钟,看起来像这样: 纬度:北纬40°42′51〃经度:西经74°00′21〃 应该是一些数学: (int)37.33168 => 37 37.33168 % 1 = 0.33168 0.33168 * 60 = 19.905 => 19 19

在android(java)中,当使用函数getlatitude()等获取当前纬度和经度时,将以十进制格式获取坐标:

纬度:24.3454523经度:10.123450

我想把它转换成度数和十进制分钟,看起来像这样:

纬度:北纬40°42′51〃经度:西经74°00′21〃

应该是一些数学:

(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,我不知道

请参考以下内容:


要将小数转换为度,您可以执行以下操作

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

根据您的要求尝试不同的选项

您有一个以十进制度数表示的坐标,这种表示格式称为“DEG”

您需要一度到DMS(度、分、秒)(例如40°42′51〃N),
转换

此java代码实现位于


如果DEG坐标值小于0,则经度为西,纬度为南。

如前所述,需要进行一些字符串操作。我创建了以下helper类,它将位置转换为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;
    }
}
导入android.location.location;
导入android.support.annotation.NonNull;
公共类位置转换器{
公共静态字符串getLatitudeAsDMS(位置,int-decimalPlace){
字符串strLatitude=Location.convert(Location.getLatitude(),Location.FORMAT_秒);
strLatitude=替换分隔符(strLatitude,decimalPlace);
strLatitude=strLatitude+“N”;
返回标准;
}
公共静态字符串getLongitudeAsDMS(位置,整数小数点){
字符串strlongitute=Location.convert(Location.getLongitude(),Location.FORMAT_SECONDS);
strlongite=替换分隔符(strlongite,decimalPlace);
标准长度=标准长度+W;
返回标准长度;
}
@非空
专用静态字符串替换分隔符(字符串str,int-decimalPlace){
str=str.replaceFirst(“:”,“°”);
str=str.replaceFirst(“:”,“”);
int pointIndex=str.indexOf(“.”);
int-endIndex=pointIndex+1+decimalPlace;
如果(endIndex
使用此

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) ;
}

}这是根据马丁·韦伯的答案改编的科特林版本。它还设置了正确的半球;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
    }
}
对象位置转换器{
趣味latitudeAsDMS(纬度:双精度,小数点:整数):字符串{
val方向=如果(纬度>0)“N”或“S”
var strlatude=Location.convert(纬度.absoluteValue,位置.FORMAT_秒)
strLatitude=替换分隔符(strLatitude,decimalPlace)
strLatitude+=“$direction”
返回标准
}
有趣的经度ADMS(经度:双精度,小数点:整数):字符串{
val方向=如果(经度>0)“W”或“E”
var strlongitute=Location.convert(longitude.absoluteValue,Location.FORMAT_SECONDS)
strlongite=替换分隔符(strlongite,decimalPlace)
标准长度+=“$方向”
返回标准长度
}
专用分隔符(str:String,decimalPlace:Int):String{
var str=str
str=str.replaceFirst(“:”.toRegex(),“”)
str=str.replaceFirst(“:”.toRegex(),“”)
val pointIndex=str.indexOf(“.”)
val endIndex=点索引+1+小数位数
if(endIndex
我想要类似的东西,在研究了这里的答案和Wikipedia之后,发现有一个坐标格式标准,它描述了坐标所需的文本表示形式,考虑到并希望在Kotlin中有一个可移植且紧凑的实现,我以这段代码结尾,希望对其他人有用,

import kotlin.math.abs
import java.util.Locale

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(" ") { (degree: Double, direction: String) ->
    val minutes = ((degree - degree.toInt()) * 60).toInt()
    val seconds = ((degree - degree.toInt()) * 3600 % 60).toInt()
    "%d°%02d′%02d″%s".format(Locale.US, degree.toInt(), minutes, seconds, direction)
} + (alt?.let { " %s%.1fm".format(Locale.US, if (alt < 0) "−" else "", abs(alt)) } ?: "")
导入kotlin.math.abs
导入java.util.Locale
fun formatCoordinateISO6709(lat:Double、long:Double、alt:Double?=null)=列表(
abs(横向)至if(横向>=0)“N”或“S”,abs(长)至if(长>=0)“E”或“W”
).joinToString(“”{(度:双精度,方向:字符串)->
val minutes=((度-度.toInt())*60.toInt())
val seconds=((度数-度数.toInt())*3600%60).toInt()
“%d°%02d′%02d〃%s”。格式(Locale.US,degree.toInt(),分,秒,方向)
}+(alt?.let{“%s%.1fm.”格式(Locale.US,if(alt<0))−" else“”,abs(alt))}?:“”)

Check这个更新了我的答案,在问题被改进为想要转换为DMS格式后。这对我来说很有效,但我必须做很多子字符串才能将格式转换为N S E W格式。。。