2014-01-27 4 views
8

In Android (Java), wenn Sie die aktuelle geografische Breite und Länge mit der Funktion getLatitude() usw. erhalten erhalten Sie die Koordinaten in Dezimal-Format:Wie formatiert man GPS Breite und Länge?

Breite: 24,3454523 Länge: 10,123450

Ich möchte so etwas wie dies dies in Grad und Dezimalminuten zu bekommen suchen:

Latitude: 40 ° 42'51 "N Längengrad: 74 ° 00'21" W

+0

prüfen diese http://Paketüberfluss.com/questions/8851816/konvertieren-dezimal-koordinieren-in-grad-minuten-sekunden-richtung –

Antwort

16

von Dezimalzahlen in Grad conver können Sie tun, folgen als

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

Referenz android developer site ist.

bearbeiten

Ich habe folgende Sache ausprobiert und die Ausgabe erhalten:

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 

andere Option Versuchen Sie, wie pro Ihre Anforderung

+0

Dies funktioniert nicht. Es ist immer noch im Dezimalformat:/ – user3182266

+0

Dies funktionierte für mich, aber ich musste eine Menge Teilstring-tun, um das Format in NSEW-Format zu bekommen ... – marienke

0

Sie haben eine in Dezimalgraden koordinieren, um dieses Darstellungsformat heißt "DEG"

Und Sie wollen eine DEG nach DMS (Grad, Minuten, Sekunden) (z.B. 40 ° 42'51 "N),
Umwandlung.

Diese Java-Code-Implementierung bei http://en.wikipedia.org/wiki/Geographic_coordinate_conversion

wenn die DEG Koordinatenwerten ist < 0, ist es für West-Länge oder Süden für die Breite.

+0

Aktualisiert meine Antwort nach Frage wurde verbessert, um in DMS-Format konvertieren möchten . – AlexWien

4

Wie bereits erwähnt, sind einige String-Manipulationen erforderlich. Ich habe die folgende Hilfsklasse, die den Standort zu DMS-Format konvertiert und erlaubt es, die Dezimalstellen für die Sekunden angeben:

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

Diese Gestaltung verwenden

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

}