Search code examples
androidgpsformattingcoordinates

How to format GPS latitude and longitude?


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


Solution

  • 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