2016-07-24 11 views
6

chcę konwertować szerokość geograficzną, długość geograficzną 40.7127837 -74.0059413 i następującym formacieAndroid Jak konwertować do formatu Szerokość Długość Stopnia

N 40 ° 42'46.0218" W 74 ° 0'21.3876"

Jaki jest najlepszy sposób na zrobienie tego?

Próbowałem metod takich jak location.FORMAT_DEGREES, location.FORMAT_MINUTES i location.FORMAT_SECONDS, ale nie jestem pewien, jak przekonwertować je do właściwego formatu. Dzięki.

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

Odpowiedz

9

Sposób Location.convert() że używasz daje bardzo dobre rezultaty i jest dobrze wdrożony i przetestowany . Trzeba tylko do formatowania wyjścia do własnych potrzeb:

private String convert(double latitude, double longitude) { 
    StringBuilder builder = new StringBuilder(); 

    if (latitude < 0) { 
     builder.append("S "); 
    } else { 
     builder.append("N "); 
    } 

    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("\""); 

    builder.append(" "); 

    if (longitude < 0) { 
     builder.append("W "); 
    } else { 
     builder.append("E "); 
    } 

    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("\""); 

    return builder.toString(); 
} 

Po wywołaniu tej metody z wejściem współrzędne:

String locationString = convert(40.7127837, -74.0059413); 

otrzymasz wynik:

N 40°42'46.02132" W 74°0'21.38868" 
+0

dzięki, to działa świetnie. – Julia

2

Jeśli w obliczu problemów z metod wbudowanych, zawsze można stworzyć własną metodę:

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

Dlaczego otoczyłeś implementację słowem 'try ... catch'? Nie mogło dojść do urządzenia przez zero lub coś podobnego, więc myślę, że blok catch nigdy nie zostanie wykonany. – Cilenco

Powiązane problemy