2014-01-27 126 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) ; 
} 

}