Android How to Convert Latitude Longitude into Degree format
Asked Answered
C

2

10

I want to convert latitude 40.7127837, longitude -74.0059413 and to the following format

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

What is the best way to do that?

I tried methods like location.FORMAT_DEGREES, location.FORMAT_MINUTES and location.FORMAT_SECONDS, but I'm not sure how to convert them to the right format. Thanks.

strLongitude = location.convert(location.getLongitude(), location.FORMAT_DEGREES);
strLatitude = location.convert(location.getLatitude(), location.FORMAT_DEGREES);
Cineraria answered 24/7, 2016 at 1:17 Comment(0)
H
19

The Location.convert() method that you are using gives very good results and is well implemented and tested. You just need to format the output to suit your needs:

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

When you call this method with your input coordinates:

String locationString = convert(40.7127837, -74.0059413);

You will receive this output:

N 40°42'46.02132" W 74°0'21.38868"
Horny answered 24/7, 2016 at 10:51 Comment(1)
Location.convert(Math.abs(latitude), Location.FORMAT_SECONDS) - This method have issues with default locale.Assuming
D
7

If you're facing problems with the inbuilt methods, you can always create your own method:

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) ;
    }
}
Disaffiliate answered 24/7, 2016 at 3:49 Comment(1)
Why have you surrounded the implementation by try...catch? It could not come to a device by zero or something like this so I think the catch block is never executed.Casias

© 2022 - 2024 — McMap. All rights reserved.