How to format GPS latitude and longitude?

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

Latitude: 24.3454523 Longitude: 10.123450

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

Latitude: 40 ° 42'51 "N Longitude: 74 ° 00'21" W

+10
android formatting coordinates gps
source share
6 answers

To convert decimals to degrees, you can do it as follows

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

Link to the Android developer site .

Edit

I tried the 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 another option as per your requirement.

+16
source share

Must be 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)

EDIT: Maybe there is an API that I don't know.

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

Convert latitude and longitude (degrees) to double. Java

+5
source share

As already mentioned, some string manipulations are needed. I created the following helper class that converts the location to DMS format and allows you to specify decimal places for 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; } } 
+4
source share

Use it

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

}

+2
source share

Here's a version of Kotlin, adapted from Martin Weber's answer. He also sets the right 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 } } 
+2
source share

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

And you want DEG for DMS (degrees, minutes, seconds) (e.g. 40 ° 42'51 "N),
conversion.

This implementation of Java code is at http://en.wikipedia.org/wiki/Geographic_coordinate_conversion

if the DEG coordinates are <0, this is West for longitude or South for latitude.

0
source share

All Articles