My application has separate algorithms to fetch data for scroll change and on user location change . For location change am using com.google.android.gms.location.LocationListener
Which is working fine.
But for on user scroll, I am getting mMap.setOnCameraChangeListener(new OnCameraChangeListener()
. But the issue is com.google.android.gms.location.LocationListene also triggers mMap.setOnCameraChangeListener(new OnCameraChangeListener().
So how to distinguish. Presently I am using Boolean values to differentiate, but it's not reliable and dirty.
I had the same issue, was trying all sorts of stuff like wrapping the view to intercept touches, or wrapping any calls to change the viewport and listening to when it started and stopped changing so I could tell if something else changed it, or matching the current viewport to what I last set it to... it's all rather fiddly. Then someone pointed me at this, and it looks promising update: has now worked flawlessly for us for months without a single issue:
map.setOnCameraMoveStartedListener { reasonCode ->
if (reasonCode == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
// I will no longer keep updating the camera location because
// the user interacted with it. This is my field I check before
// snapping the camera location to the latest value.
tracking = false
}
}
I doubt that there's an easy, reliable, and real-time way to do this. IOW, I suspect that it is going to be "dirty".
If I had to try, I would supply my own LocationSource
, so that I knew when the map would be changing based upon position. I would then try to ignore the next call (calls?) to my OnCameraChangeListener
, as being ones tied to the location change.
(BTW, note that LocationListener
was deprecated)
Or, I would try to change my data-fetching algorithm to treat all camera changes equally. After all, if my objective is to make sure that I have data on all sides of the map from the current center, it doesn't matter how I got to that center, just so long as I am there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With