2014-01-27 98 views
8

在安卓(JAVA)当你)当前的纬度和经度使用功能getlatitude(等你在十进制格式的坐标:如何格式化GPS经纬度?

纬度:24.3454523经度:10.123450

我想进入度和小数分这是这个样子:

北纬:40°42'51“N经度:74°00'21”W

+0

入住此http:/ /堆栈溢出。com/questions/8851816/convert-decimal-coordinate-into-degrees-minutes-seconds-direction –

回答

16

从小数到度CONVER你可以做如下

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

引用android developer site

编辑

我曾尝试下面的事情,得到的输出:

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 

尝试不同的选项,按您的要求

+0

这不起作用。它仍然是十进制格式:/ – user3182266

+0

这对我有效,但我不得不做很多子串才能将格式转换为NSEW格式... – marienke

0

你必须以十进制度的坐标,这表示格式被称为“DEG”

而你想要一个DEG到DMS(度,分,秒) (例如40°42'51“N),
转换。

http://en.wikipedia.org/wiki/Geographic_coordinate_conversion

如果DEG坐标值这个java代码实现< 0,这是西经度或南纬度。

+0

在问题被改进以想要转换成DMS格式后更新了我的答案。 – AlexWien

4

如前所述,需要一些字符串操作。我创建了下面的辅助类,其位置转换到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; 
    } 
} 
0

使用此

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

}