17

In android(java) when you get the current latitude and longitude using the function getlatitude() etc you get the coordinates in decimal format:

latitude: 24.3454523 longitude: 10.123450

I want to get this into degrees and decimal minutes to look something like this:

Latitude: 40°42′51″ N Longitude: 74°00′21″ W

WestDiscGolf
  • 4,098
  • 2
  • 32
  • 47
user3182266
  • 1,270
  • 4
  • 23
  • 49
  • Check this http://stackoverflow.com/questions/8851816/convert-decimal-coordinate-into-degrees-minutes-seconds-direction – Zohra Khan Jan 27 '14 at 13:37

8 Answers8

21

to conver from decimals to degrees you can do as follow

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

reference is android developer site.

Edit

I have tried following thing and get the output:

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

Try different option as per your requirement

dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
  • This worked for me, but I had to do a lot of substring-ing to get the format into N S E W format... – marienke Mar 17 '15 at 13:45
6

Should be some math:

(int)37.33168                => 37

37.33168 % 1 = 0.33168
0.33168 * 60 = 19.905        => 19

19.905 % 1 = 0.905    
0.905 * 60                   => 54

same with -122 (add 360 if negative value)

EDIT: May be there is some API, which I don't know.

Refer From: How to find degree from the latitude value in android?

Convert Latitude and Longitude Values (Degrees) to Double . Java

Community
  • 1
  • 1
mshoaiblibra
  • 390
  • 4
  • 15
6

Here's a Kotlin version, adapted from Martin Weber's answer. It also sets the correct hemisphere; N, S, W or E

object LocationConverter {

    fun latitudeAsDMS(latitude: Double, decimalPlace: Int): String {
        val direction = if (latitude > 0) "N" else "S"
        var strLatitude = Location.convert(latitude.absoluteValue, Location.FORMAT_SECONDS)
        strLatitude = replaceDelimiters(strLatitude, decimalPlace)
        strLatitude += " $direction"
        return strLatitude
    }

    fun longitudeAsDMS(longitude: Double, decimalPlace: Int): String {
        val direction = if (longitude > 0) "W" else "E"
        var strLongitude = Location.convert(longitude.absoluteValue, Location.FORMAT_SECONDS)
        strLongitude = replaceDelimiters(strLongitude, decimalPlace)
        strLongitude += " $direction"
        return strLongitude
    }

    private fun replaceDelimiters(str: String, decimalPlace: Int): String {
        var str = str
        str = str.replaceFirst(":".toRegex(), "°")
        str = str.replaceFirst(":".toRegex(), "'")
        val pointIndex = str.indexOf(".")
        val endIndex = pointIndex + 1 + decimalPlace
        if (endIndex < str.length) {
            str = str.substring(0, endIndex)
        }
        str += "\""
        return str
    }
}
5

As already mentioned, there are some string manipulations required. I created the following helper class, which converts the location to DMS format and allows to specify the decimal places for the seconds:

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;
    }
}
Martin
  • 818
  • 9
  • 20
4

Use This

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

}

abi
  • 1,002
  • 12
  • 15
1

I wanted something similar and after studying answers here and Wikipedia, found there is a standard for formatting coordinates, ISO 6709#Annex D which describes the desired text representation for coordinates, considering that and wanting to have a portable and compact implementation in Kotlin, I ended with this code hopefully would be useful for others,

import kotlin.math.abs
import java.util.Locale

fun formatCoordinateISO6709(lat: Double, long: Double, alt: Double? = null) = listOf(
    abs(lat) to if (lat >= 0) "N" else "S", abs(long) to if (long >= 0) "E" else "W"
).joinToString(" ") { (degree: Double, direction: String) ->
    val minutes = ((degree - degree.toInt()) * 60).toInt()
    val seconds = ((degree - degree.toInt()) * 3600 % 60).toInt()
    "%d°%02d′%02d″%s".format(Locale.US, degree.toInt(), minutes, seconds, direction)
} + (alt?.let { " %s%.1fm".format(Locale.US, if (alt < 0) "−" else "", abs(alt)) } ?: "")
Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
0

You have a coordinate in decimal degrees, this representation format is called "DEG"

And you want a DEG to DMS (Degrees, Minutes, Seconds) (e.g 40°42′51″ N) ,
conversion.

This java code implementation at http://en.wikipedia.org/wiki/Geographic_coordinate_conversion

if the DEG coordinate values is < 0, it is West for longitude or South for latitude.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

use this method and pass the cordinates to the method

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


    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("\"");
    if (latitude < 0) {
        builder.append("S ");
    } else {
        builder.append("N ");
    }

    builder.append("  ");


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

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

    return builder.toString();
}
Nikit Dungrani
  • 185
  • 1
  • 7