Android Question [SOLVED] FusedLocationProvider, what is the trigger for LocationChanged?

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have this codes on LocationChanged event
B4X:
Private Sub flp_LocationChanged (Location1 As Location)
    If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
       Private b As Beeper
       b.Initialize(300, 1000) '300 milliseconds, 500 hz
       b.Beep
    End If
End Sub

I put my phone on a table, every 10 secs, it beep a sound.
It seem that even phone location was not changed, this event is always triggered.
 

DonManfred

Expert
Licensed User
Longtime User
Instead of just checking the time i would add a check for the location has really changed or not. Only beep if.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
The beep was only for test.

I was hoping that the event wouldn't be triggered when location did not change, seem that I was wrong.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Log the location and check whether it changes or not. There are configurable settings that affect the change thresholds.
I changed the codes into these
B4X:
Private Sub flp_LocationChanged (Loc As Location)
   If Tag = 0 Then
        Tag = 1
        LogColor(Loc.Latitude,Colors.Green)
        LogColor(Loc.Longitude,Colors.Green)
        LogColor(Loc.Accuracy,Colors.Green)
        LogColor(Loc.Speed,Colors.Green)
    Else
        Tag = 0
        LogColor(Loc.Latitude,Colors.Yellow)
        LogColor(Loc.Longitude,Colors.Yellow) 
        LogColor(Loc.Accuracy,Colors.Yellow)
        LogColor(Loc.Speed,Colors.Yellow)
    End If
End Sub

The phone was on the table and not moved, on log windows, latitude & longitude were exactly the same, but the event still triggered.
Speed showed : 0
Accuracy showed : 16

Could it be because GPS signal got disconnected due to weak signal and then connected again when signal got stronger again?

Where are the configurable settings?

Tip: beepers object should be used. Your code will create a big memory leak.
Although I will remove the beepers when testing no longer needed, but could you give me a sample code how to use beepers object?
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The location will often change even if you are standing still because it will change even for .0000001 degree difference.

And you will see the accuracy change too, which also causes the lat/long to change too.
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
The location will often change even if you are standing still because it will change even for .0000001 degree difference.

And you will see the accuracy change too, which also causes the lat/long to change too.
Like Erel's said, are there a settings to configure so that the event only be triggered when there is a change for, let say 1 degree?​
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
To specify the minimum distance that needs to happen before an event is triggered - you need to modify the locationrequest:

B4X:
    Dim lr As LocationRequest
    ....
    lr.SetSmallestDisplacement(100)   '100 meter change in location needed to trigger event
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
To specify the minimum distance that needs to happen before an event is triggered - you need to modify the locationrequest:

B4X:
    Dim lr As LocationRequest
    ....
    lr.SetSmallestDisplacement(100)   '100 meter change in location needed to trigger event
Where is the place to declare variable lr? In process global, global, or in sub location changed?

And where to call lr.SetSmallestDisplacement(100)? In service start, service create?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Put it in the createlocationrequest sub:
B4X:
Private Sub CreateLocationRequest As LocationRequest
    Dim lr As LocationRequest
    lr.Initialize
    lr.SetSmallestDisplacement(100)   '<-------------- add this line
    lr.SetInterval(10000)
    lr.SetFastestInterval(lr.GetInterval / 2)
    Return lr
End Sub
 
Last edited:
Upvote 0
Top