Android 如何格式化GPS经纬度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21382230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:28:05  来源:igfitidea点击:

How to format GPS latitude and longitude?

androidgpsformattingcoordinates

提问by user3182266

In android(java) when you get the current latitude and longitude using the function getlatitude() etc you get the coordinates in decimal format:

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

latitude: 24.3454523 longitude: 10.123450

纬度:24.3454523 经度:10.123450

I want to get this into degrees and decimal minutes to look something like this:

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

Latitude: 40°42′51″ N Longitude: 74°00′21″ W

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

回答by dinesh sharma

to conver from decimals to degrees you can do as follow

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

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

reference is android developer site.

参考是android 开发者网站

Edit

编辑

I have tried following thing and get the output:

我尝试了以下内容并获得输出:

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

Try different option as per your requirement

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

回答by mshoaiblibra

Should be some math:

应该是一些数学:

(int)37.33168                => 37

37.33168 % 1 = 0.33168
0.33168 * 60 = 19.905        => 19

19.905 % 1 = 0.905    
0.905 * 60                   => 54

same with -122 (add 360 if negative value)

与 -122 相同(如果为负值,则添加 360)

EDIT:May be there is some API, which I don't know.

编辑:可能有一些我不知道的 API。

Refer From:How to find degree from the latitude value in android?

参考来源:如何从android中的纬度值中找到度数?

Convert Latitude and Longitude Values (Degrees) to Double . Java

将纬度和经度值(度)转换为 Double 。爪哇

回答by Martin

As already mentioned, there are some string manipulations required. I created the following helper class, which converts the location to DMS format and allows to specify the decimal places for the seconds:

如前所述,需要进行一些字符串操作。我创建了以下帮助程序类,它将位置转换为 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;
    }
}

回答by planetmik

Here's a Kotlin version, adapted from Martin Weber's answer. It also sets the correct hemisphere; N, S, W or E

这是一个 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
    }
}

回答by abi

Use This

用这个

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

}

}

回答by AlexWien

You have a coordinate in decimal degrees, this representation format is called "DEG"

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

And you want a DEG to DMS (Degrees, Minutes, Seconds) (e.g 40°42′51″ N) ,
conversion.

并且您需要 DEG 到 DMS(度、分、秒)(例如 40°42′51″ N)的
转换。

This java code implementation at http://en.wikipedia.org/wiki/Geographic_coordinate_conversion

这个java代码实现在http://en.wikipedia.org/wiki/Geographic_coordinate_conversion

if the DEG coordinate values is < 0, it is West for longitude or South for latitude.

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