Android Question GeoLoc.GetLocation To degrees

ciginfo

Well-Known Member
Licensed User
Longtime User
Hello,
With Geolocation, I get with GeoLoc.GetLocation a double. How to convert this double to Degrees.
Thank you
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private NativeMe As JavaObject
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        NativeMe.InitializeContext
    End If
    Dim geolat As Double = 50.8248068
    Dim geolon As Double = 6.4326403
    Dim degrees As String = NativeMe.RunMethod("getFormattedLocationInDegree", Array As Object(geolat, geolon))
    Log($"Degree = ${degrees}"$)
    'Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub

#If JAVA
import android.location.Location;
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) ;
    }
}

public String convertLAT2degrees(double lat) {
    String strLatitude = Location.convert(lat, Location.FORMAT_DEGREES);
     return strLatitude;
}
public String convertLON2degrees(double lon) {
    String strLongitude = Location.convert(lon, Location.FORMAT_DEGREES);
    return strLongitude;
}
public String convertLAT2seconds(double lat, int decimalPlace) {
    String strLatitude = Location.convert(lat, Location.FORMAT_SECONDS);
  strLatitude = replaceDelimiters(strLatitude, decimalPlace);
  strLatitude = strLatitude + " N";
    return strLatitude;
}
public String convertLON2seconds(double lon, int decimalPlace) {
    String strLongitude = Location.convert(lon, Location.FORMAT_SECONDS);
    strLongitude = replaceDelimiters(strLongitude, decimalPlace);
    strLongitude = strLongitude + " W";
    return strLongitude;
}
public String convertLAT2minutes(double lat) {
    String strLatitude = Location.convert(lat, Location.FORMAT_MINUTES);
    return strLatitude;
}
public String convertLON2minutes(double lon) {
    String strLongitude = Location.convert(lon, Location.FORMAT_MINUTES);
    return strLongitude;
}
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;
}

#End If

Degree = 50°49'29"N 6°25'58"E
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private rp As RuntimePermissions
    Private NativeMe As JavaObject     
End Sub

Sub Globals
    Public GeoLoc As GeoLocator
    Dim MyLatitude, MyLongitude As Double
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    wait for Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        GeoLoc.Initialize("GeoLocation")
        GeoLoc.GetLocation
        GeoLoc.geoAddress
        LblAdress.Text = GeoLoc.Address
        
        MyLatitude = GeoLoc.Lattitude
        MyLongitude = GeoLoc.Longitude
''    xxxConvertitt en degres xxxx
            NativeMe.InitializeContext
            Dim degrees As String = NativeMe.RunMethod("getFormattedLocationInDegree", Array As Object(MyLatitude, MyLongitude))
            Log($"Degree = ${degrees}"$)
    Else
        Log("NoPermission")
    End If
It no runs, I obtain error:
java.lang.RuntimeException: Method: getFormattedLocationInDegree not found in: ciginfo.Join_Me_New.main
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
You probably missing JAVA code?
B4X:
#If JAVA
import android.location.Location;
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) ;
    }
}

public String convertLAT2degrees(double lat) {
    String strLatitude = Location.convert(lat, Location.FORMAT_DEGREES);
     return strLatitude;
}
public String convertLON2degrees(double lon) {
    String strLongitude = Location.convert(lon, Location.FORMAT_DEGREES);
    return strLongitude;
}
public String convertLAT2seconds(double lat, int decimalPlace) {
    String strLatitude = Location.convert(lat, Location.FORMAT_SECONDS);
  strLatitude = replaceDelimiters(strLatitude, decimalPlace);
  strLatitude = strLatitude + " N";
    return strLatitude;
}
public String convertLON2seconds(double lon, int decimalPlace) {
    String strLongitude = Location.convert(lon, Location.FORMAT_SECONDS);
    strLongitude = replaceDelimiters(strLongitude, decimalPlace);
    strLongitude = strLongitude + " W";
    return strLongitude;
}
public String convertLAT2minutes(double lat) {
    String strLatitude = Location.convert(lat, Location.FORMAT_MINUTES);
    return strLatitude;
}
public String convertLON2minutes(double lon) {
    String strLongitude = Location.convert(lon, Location.FORMAT_MINUTES);
    return strLongitude;
}
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;
}

#End If
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
Yes. Where I place it? Is it a module? I place it into the Main ? I'm lost!!!!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
See this Tutorial

 
Upvote 0

Serge Bertet

Active Member
Licensed User
Without JavaObject:
B4X:
' From android.location.Location 'String getFormattedLocationInDegree'
Sub getFormattedLocationInDegree(loc As Double) As String
    Dim Seconds As Int = Round(loc * 3600)
    Dim degrees As Int = Seconds / 3600
    Seconds = Abs(Seconds Mod 3600)
    Dim minutes As Int = Seconds / 60
    Seconds = Seconds Mod 60
    Return IIf(degrees >= 0, "+", "-") & degrees & "°" & minutes & "'" & Seconds & $"""$
End Sub

' Returns something like 0°53'50"E
Sub getFormattedLongitudeInDegree(lon As Double) As String
    Dim rep As String = getFormattedLocationInDegree(lon)
    Return rep.SubString(1) & IIf(rep.StartsWith("-"), "W", "E")
End Sub

' Returns something like : 45°52'59"N
Sub getFormattedLatitudeInDegree(lat As Double) As String
    Dim rep As String = getFormattedLocationInDegree(lat)
    Return rep.SubString(1) & IIf(rep.StartsWith("-"), "S", "N")
End Sub
Just written, needs to be tested, it works for me 'til now.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…