2010-11-18 11 views

Odpowiedz

7

powinno być pewne matematyka:

(int)37.33168    => 37 
37.33168 % 1 = 0.33168  
0.33168 * 60 = 19,905  => 19 
19.905 % 1 = 0.905 
0.905 * 60     => 54 

samo z -122 (dodaj 360, jeśli wartość nagative)

EDIT: Może istnieją pewne API, które nie wiem.

+0

Dziękuję za wyjaśnienie matematyczne. :) – theapache64

2

Zapoznaj się także z dokumentacją klasy Location, szczególnie w metodzie convert(), ponieważ powinna ona robić to, co chcesz.

11

To daje Strings w stopniach, minutach i sekundach łuku:

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

.

8

użycie tego

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

odpowiedź Abi działa bardzo dobrze na moim miejscu, ale produkuje błędy w niektórych innych, na przykład "E" zamiast "W" na zachodniej półkuli. IMO powinno być:

String lonDegrees = longDegrees >= 0 ? "E" : "W"; 

zamiast:

String lonDegrees = latDegrees >= 0 ? "E" : "W"; 
0

Innym sposobem można osiągnąć to za pomocą następujących funkcji

private String convertCoordinate(double coordinate, boolean isLatitude){ 
    String[] pString = isLatitude ? new String[]{"N", "S"} : new String[]{"E", "W"}; 
    int degree = (int) coordinate; 
    int minute = (int) (Math.abs(coordinate) * 60) % 60; 
    int second = (int) (Math.abs(coordinate) * 3600) % 60; 

    int index = degree < 0 ? 1 : 0; 
    degree = Math.abs(degree); 

    return degree + pString[index] + " " + minute + "' " + second + "\""; 
} 
0

podstawie SO odpowiedzi zrobiłem kod, który przelicz DEG na DMS z FORMATEM

przykład 40 ° 42'51 "N 74 ° 00'21" W

przykład połączenia przychodzącego getLocationAsDMS (położenie 8) 8 jest odpowiedni numer tego formatu

public String getLocationAsDMS (Location location, int decimalPlace){ 
    String strLatitude = Location.convert(location.getLatitude(), Location.FORMAT_SECONDS); 
    strLatitude = replaceDelimiters(strLatitude, decimalPlace); 
    char latCardinal = location.getLongitude() >= 0 ? 'N' : 'S'; 
    strLatitude = strLatitude + " " + latCardinal; 

    String strLongitude = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS); 
    strLongitude = replaceDelimiters(strLongitude, decimalPlace); 
    char lonCardinal = location.getLongitude() >= 0 ? 'E' : 'W'; 
    strLongitude = strLongitude + " " + lonCardinal; 

    return strLatitude + " " + strLongitude; 
} 

@NonNull 
private 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; 
} 
Powiązane problemy