Android Question GeoLocation works but constantly fails

Similar threads

B4A Code Snippet Upload files with WebView
B4A Tutorial Google Maps
B4A Tutorial Geofence - Monitoring a region in the background
B4A Tutorial Background location tracking
B4A Library GeoLocation

JohnC

Well-Known Member
Licensed User
Please provide more info on why you are using the lib and how do you know it fails.
 

anthonyx0

Member
Licensed User
I am using the library to get the latitude and longitude. I know it fails because on occasions it saves the coordinates well but on other occasions it shows me the following error;
I have this in my configuration I am using B4A 9.80, android 6, I tested it on 7,8,9 and it never worked
<uses-sdk android: minSdkVersion = "16" android: targetSdkVersion = "29" />
 

Attachments

JohnC

Well-Known Member
Licensed User
OK, this might not be a geolocation lib issue because it looks like the error is happening in your code (MAIN on line 33).

Can you post the code in the "Main" module so we can see the code leading up to line 33.
 

JohnC

Well-Known Member
Licensed User
Rarely do examples include robust error checking.

So, Don's example seems to assume that the GeoLoc.GetLocation function will always succeed, so it's then ok to immediately invoke the GeoLoc.geoAddress function to get its address.

But if the GetLocation fails, then it will generate an error when you try to get the address because there is no valid lat/long.

So, try changing the code to these TWO subs so that it wont try to get an address unless the getlocation succeeded:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    '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
    Else
        Log("NoPermission")
    End If
End Sub

Sub GeoLocation_Location(success As Boolean, Lattitude As Double, Longitude As Double, info As String, isMockLocation As Boolean)
    Log($"GeoLocation_Location(${success}, ${Lattitude}, ${Longitude}, ${info}, ${isMockLocation})"$)

    If Success = True Then
        GeoLoc.geoAddress
        Log(GeoLoc.Address)
        Log(GeoLoc.City)
        Log(GeoLoc.Country)
        Log(GeoLoc.KnownName)
        Log(GeoLoc.PostalCode)
        Log(GeoLoc.State)
    End If

End Sub
 
Last edited:

JohnC

Well-Known Member
Licensed User
Also, I just noticed this exact error and the same fix was posted in the thread of that lib.

In Post #4, and Post #8.
 
Top