Android Question FusedLocation And GPS And LocationProvider

A Z M JANNAT UL KARIM

Member
Licensed User
Hi all, one of my application using the FusedLocation to get the current location and everything fine until I saw that the person is far away from the exact location. When I check the accuracy I found that it is 79.... meter (I suppose). Now I locked it to get only get the location when it is < 30 meter. Currently the problem is sometimes when my app run it didn't get any location at all and log shows more than 30 meter that's why it is not showing. That is OK.

Now how can I overcome the current situation? Should I need to use all the libraries to get the location, ie, FusedLocation And GPS And LocationProvider. If so how to integrate those with some sample code will be very much appreciable. Thank you in advance :)
 

A Z M JANNAT UL KARIM

Member
Licensed User
Thanks Erel, Is there any standard code through which I can get apprx accurate location. I had followed the below link

https://www.b4x.com/android/forum/threads/fusedlocationprovider.50614/

But I am confused is that OK ? Sometimes as I said earlier no location provided as the accuracy greater then 30 meter.

Following is my code ...

B4X:
Sub Process_Globals
    Private FusedLocationProvider1 As FusedLocationProvider
    Private LastLocation As Location
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        FusedLocationProvider1.Initialize("FusedLocationProvider1")
    End If
End Sub

Sub Activity_Resume
    Starter.reqForPerm.CheckAndRequest(Starter.reqForPerm.PERMISSION_ACCESS_COARSE_LOCATION)
    FusedLocationProvider1.Connect
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    FusedLocationProvider1.Disconnect
End Sub

Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
    Log("FusedLocationProvider1_ConnectionFailed " & ConnectionResult1)
    
    '    the FusedLocationProvider ConnectionResult object contains the various CoonectionResult constants
    
    Select ConnectionResult1
        Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
            '    a network error has occurred, this is likely to be a recoverable error
            '    so try to connect again
            FusedLocationProvider1.Connect
        Case Else
            '    TODO handle other errors
    End Select
End Sub

Sub FusedLocationProvider1_ConnectionSuccess
    Log("FusedLocationProvider1_ConnectionSuccess")
    Dim LocationRequest1 As LocationRequest
    LocationRequest1.Initialize
    LocationRequest1.SetInterval(1000)    '    1000 milliseconds
    LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
    LocationRequest1.SetSmallestDisplacement(10)    '    1 meter

    Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
    LocationSettingsRequestBuilder1.Initialize
    LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest1)
    FusedLocationProvider1.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)

    FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
End Sub

Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
    Log("FusedLocationProvider1_ConnectionSuspended")
    
    '    the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
    
    Select SuspendedCause1
        Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
            '    TODO take action
        Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
            '    TODO take action
    End Select
End Sub

Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    ' Log("FusedLocationProvider1_LocationChanged")
    LastLocation=Location1
    UPDATE_LOCATION_SETT
End Sub

Sub FusedLocationProvider1_LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
    Log("FusedLocationProvider1_LocationSettingsChecked")
    Dim LocationSettingsStatus1 As LocationSettingsStatus=LocationSettingsResult1.GetLocationSettingsStatus
    Select LocationSettingsStatus1.GetStatusCode
        Case LocationSettingsStatus1.StatusCodes.RESOLUTION_REQUIRED
            Log("RESOLUTION_REQUIRED")
            '    device settings do not meet the location request requirements
            '    a resolution dialog is available to enable the user to change the settings
            LocationSettingsStatus1.StartResolutionDialog("LocationSettingsResult1")
        Case LocationSettingsStatus1.StatusCodes.SETTINGS_CHANGE_UNAVAILABLE
            Log("SETTINGS_CHANGE_UNAVAILABLE")
            '    device settings do not meet the location request requirements
            '    a resolution dialog is not available to enable the user to change the settings
            Msgbox("Unable to listen for location updates, device does not meet the requirements.", "Problem")
            ' Activity.Finish
        Case LocationSettingsStatus1.StatusCodes.SUCCESS
            Log("SUCCESS")
            '    device settings meet the location request requirements
            '    no further action required
    End Select
End Sub

Sub LocationSettingsResult1_ResolutionDialogDismissed(LocationSettingsUpdated As Boolean)
    Log("LocationSettingsResult1_ResolutionDialogDismissed")
    If Not(LocationSettingsUpdated) Then
        '    the user failed to update the device settings to meet the location request requirements
        Msgbox("Unable to listen for location updates, you failed to enable the required device settings.", "Problem")
        ' Activity.Finish
    End If
End Sub

Sub UPDATE_LOCATION_SETT
    If LastLocation.Accuracy < 30 Then
        Starter.DEV_LAT_STAT=LastLocation.Latitude
        Starter.DEV_LNG_STAT=LastLocation.Longitude
        Starter.DEV_ALT_STAT = LastLocation.Altitude
        Starter.DEV_ACC_STAT=LastLocation.Accuracy
        Starter.DEV_LOC_PROV = ""
        Starter.DEV_LOC_LAST_ACC_TM = DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now)

        Dim LocationRequest1 As LocationRequest
        LocationRequest1.Initialize
        LocationRequest1.SetInterval(30000)
        LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
        LocationRequest1.SetSmallestDisplacement(10) 

        Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
        LocationSettingsRequestBuilder1.Initialize
        LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest1)
        FusedLocationProvider1.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)

        FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
    Else
        Log("Invalid Accuracy !!! " & LastLocation.Accuracy)
        Dim LocationRequest1 As LocationRequest
        LocationRequest1.Initialize
        ' If the location accuracy high start it again ...
        LocationRequest1.SetInterval(10)    '    10 milliseconds
        LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
        LocationRequest1.SetSmallestDisplacement(10)

        Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
        LocationSettingsRequestBuilder1.Initialize
        LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest1)
        FusedLocationProvider1.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)

        FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
    End If
End Sub

Any light on this will be highly appreciable ...
 
Upvote 0

A Z M JANNAT UL KARIM

Member
Licensed User
Hi Erel, Sorry for the later reply. Was not infront of PC.

I didn't understand. Should I use
Starter.reqForPerm.CheckAndRequest(Starter.reqForPerm.PERMISSION_ACCESS_FINE_LOCATION) instead of
Starter.reqForPerm.CheckAndRequest(Starter.reqForPerm.PERMISSION_ACCESS_COARSE_LOCATION)? Or use both. A sample code will be highly appreciable.

Though I do not understand the difference between two :-( Sorry for my ignorance. Thank you in advance.
 
Upvote 0

A Z M JANNAT UL KARIM

Member
Licensed User
Got the difference from below reference :)

https://stackoverflow.com/questions...ion-already-can-i-omit-access-coarse-location

Permission wise, ACCESS_FINE_LOCATION includes ACCESS_COARSE_LOCATION. However, there is a catch:

ACCESS_COARSE_LOCATION gives you last-known location which is battery friendlyhttps://developer.android.com/training/location/retrieve-current.html#setup
For example, if your app does something like location-based recommendations, last-known location is good enough.
This has a dependency on Google Play Services

However, if you need something like live/ real-time location like Pokemon Go, use ACCESS_FINE_LOCATION
It gives you live/ real-time location. You'll need to use a LocationListener
Last time I checked, this does not require Google Play Services

Seems to be I need the ACCESS_FINE_LOCATION. Thank You.
 
Upvote 0
Top