I still don't believe I couldn't find any question on SO, so please feel free to point me to one.
I'm implementing an application using Google Maps that shows several markers. I want to make it dynamic so that only seen markers are drawn. For this, I want to be able to know when the map is completely stopped, then wait a couple of seconds so I dont mess around with the map while the user might still be moving it, and then clear markers and draw the new ones. If the user moves before the timer fires it has to cancel and then start counting once again.
So far, I managed to get the camera change to fire when the animation is stopped using onCameraChangeListener
, though it's definition specifies that this might still get called in mid animation. Is this the right way to do it?
Second question is regarding timers. My current implementation is as follows:
map.setOnCameraChangeListener(new OnCameraChangeListener() {
public void onCameraChange(CameraPosition position) {
refresher.schedule(new refreshMapData(), 2000);
}
});
And the Timer that actually updates the necessary markers is this one:
class refreshMapData extends TimerTask{
public void run() {
map.clear();
for ( ... ) {
map.addMarker( ... );
}
}
}
Which obviously throws a "Not on the main thread" exception and leads me to the next question: What is the workaround for this issue? How can I modify Google Map's values using a timer if I'm not allowed to do it from outside the main thread?
Edit: About the first question, I'm guessing I just need to compare if the position has changed since the last time so that will do. Just need the answer to the timer updating issue.