Android Question GeoLocation works but constantly fails

JohnC

Expert
Licensed User
Longtime User
Please provide more info on why you are using the lib and how do you know it fails.
 
Upvote 0

anthonyx0

Member
Licensed User
Longtime 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

  • Screenshot_3.png
    Screenshot_3.png
    34 KB · Views: 237
Upvote 0

JohnC

Expert
Licensed User
Longtime 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.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime 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:
Upvote 0

JohnC

Expert
Licensed User
Longtime 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.
 
Upvote 0
Top