Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to Convert Latitude Longitude into Degree format

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);
like image 482
Julia Avatar asked Jul 24 '16 01:07

Julia


People also ask

How do you write GPS coordinates in degrees?

Enter coordinates to find a place Here are examples of formats that work: Decimal degrees (DD): 41.40338, 2.17403. Degrees, minutes, and seconds (DMS): 41°24'12.2"N 2°10'26.5"E. Degrees and decimal minutes (DMM): 41 24.2028, 2 10.4418.

How do I set latitude and longitude on Android?

Steps to get current latitude and longitude in Android Location permissions for the manifest file for receiving the location update. Create a LocationManager instance as a reference to the location service. Request location from LocationManager. Receive location update from LocationListener on change of location.

What format is longitude?

Latitude and longitude are a pair of numbers (coordinates) used to describe a position on the plane of a geographic coordinate system. The numbers are in decimal degrees format and range from -90 to 90 for latitude and -180 to 180 for longitude. For example, Washington DC has a latitude 38.8951 and longitude -77.0364 .


1 Answers

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) ;
    }
}
like image 106
Akeshwar Jha Avatar answered Sep 21 '22 23:09

Akeshwar Jha