iOS Code Snippet Convert latitude/longitude strings to double

I searched on "convert latitude string" and found nothing comparable so here's my contribution.

If there is a built in function somewhere or a more elegant way to do this then hit the [Reply] button.

I am in the process of porting a B4A app to B4I.

One of the many differences I have found is in the area of defining GPS locations.

In B4A you can use Location.Initialize2(latitude, longitude), the documentation saying:

Initialize2 (Latitude As String, Longitude As String)

Initializes the location object with the given Latitude and Longitude.
Values can be formatted in any of the three formats:
Degrees: [+-]DDD.DDDDD
Minutes: [+-]DDD:MM.MMMMM (Minute = 1 / 60 of a degree)
Seconds: [+-]DDD:MM:SS.SSSSS (Second = 1 / 3600 of a degree)
Example:
Dim L1 As Location
L1.Initialize2("45:30:30", "45:20:15")

Being able to enter latitudes and longitudes as strings of a non-numeric nature is very handy, particularly if (as in my app) they are coming from external configuration file/s that may have been hand generated.

In B4I you use Location.Initialize2(latitude, longitude) again (at least the names of the methods match), but the documentation now says:

Initialize2 (Latitude As Double, Longitude As Double)

Creates a new Location object that can be used for distance calculations.

So, in your porting you either have to go back and change your external configuration file/s

-OR-

Maybe use this bit of code:
B4X:
'************************************************************************************
'
'This procedure converts a latitude/longitude string to a double
'
'Input parameters are:
'
'       Lat_long_string = latitude/longitude string, in any of following formats:
'                         [+-]DDD.DDDDD
'                         [+-]DDD:MM.MMMMM
'                         [+-]DDD:MM:SS.SSSSS
'
'Returns:
'
'       Numeric equivalent, as a Double
'
'Notes on this procedure:
'
'       o None
'
'************************************************************************************
Private Sub Util_convert_lat_long(Lat_long_string As String) As Double

    Private wrk_sign, wrk_DD, wrk_MM, wrk_SS As Double
    Private wrk_ptr As Int

    Lat_long_string = Lat_long_string.Trim

    'Sign stuff has to be done this way to handle cases such as -0:MM.MMMMM,
    '-:MM.MMMMM etc

    'Assume +ve
    wrk_sign = 1

    'If -ve...
    If Lat_long_string.SubString2(0,1) = "-" Then
  
        'Set sign to -ve
        wrk_sign = -1
  
        'Lop off [-]
        Lat_long_string = Lat_long_string.SubString(1)
  
    End If

    'Find location of first :
    wrk_ptr = Lat_long_string.IndexOf(":")

    'If have [+]DDD:MM.MMMMM or DDD:MM:SS.SSSSS...
    If wrk_ptr > -1 Then

        'Extract absolute DDD
        Try
            wrk_DD = Lat_long_string.SubString2(0, wrk_ptr)
        Catch
            wrk_DD = 0
        End Try

        'Lop off [+]DDD:
        Lat_long_string = Lat_long_string.SubString(wrk_ptr + 1)

        'Find location of second :
        wrk_ptr = Lat_long_string.IndexOf(":")
  
        'If have MM:SS.SSSSS...
        If wrk_ptr > -1 Then

            'Extract absolute MM
            Try
                wrk_MM = Lat_long_string.SubString2(0, wrk_ptr)
            Catch
                wrk_MM = 0
            End Try

            'Extract absolute SS.SSSSS
            Try
                wrk_SS = Lat_long_string.SubString(wrk_ptr + 1)
            Catch
                wrk_SS = 0
            End Try

        'Otherwise, must have MM.MMMMM...
        Else
  
            'Extract absolute MM.MMMMM
            Try
                wrk_MM = Lat_long_string
            Catch
                wrk_MM = 0
            End Try
      
        End If

    'Otherwise, must have [+]DDD.DDDDD...
    Else

        'Extract [+]DDD.DDDDD  
        Try
            wrk_DD = Lat_long_string
        Catch
            wrk_DD = 0
        End Try

    End If

    Return wrk_sign * (wrk_DD + wrk_MM/60 + wrk_SS/3600)

End Sub

I have only used this in B4I but there is no reason I can see why it would not work, unaltered, in B4A - although the need escapes me.

Happy coding...
 
Last edited:
Top