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

17
在安卓(java)中,使用getlatitude()等函数获取当前经纬度时,会以十进制格式返回坐标:

纬度:24.3454523 经度:10.123450

我想将其转换为度和分的十进制形式,如下所示:

纬度:40°42′51″ N 经度:74°00′21″ W


请查看此链接:https://dev59.com/z2ox5IYBdhLWcg3w_JHc - Zohra Khan
8个回答

21

将十进制转换为度数,您可以按如下方式进行操作:

String strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_DEGREES);
String strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_DEGREES);

参考 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

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


这对我有效,但是我不得不进行很多子字符串操作来将格式转换为N S E W格式... - marienke

6

正如之前提到的,需要进行一些字符串操作。我创建了以下助手类,将位置转换为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;
    }
}

6

6
这里是 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
    }
}

4

使用这个

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

}


1
我希望能找到类似的东西,经过研究这里的答案和维基百科,发现有一个格式化坐标的标准ISO 6709#Annex D,它描述了坐标的期望文本表示形式,考虑到这一点并希望在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)) } ?: "")

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

0

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

而你想要进行DEG到DMS(度,分,秒)(例如40°42′51″ N)的转换。

这个Java代码实现可以在http://en.wikipedia.org/wiki/Geographic_coordinate_conversion找到。

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


在问题改进为想要将其转换为DMS格式后,我更新了我的答案。 - AlexWien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接