3

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.

h4lc0n
  • 2,730
  • 5
  • 29
  • 41
  • can you share the whole code of this now please. I want it – Qadir Hussain Jan 06 '14 at 07:12
  • @QadirHussain Unfortunately I'm not able to access that project anymore. Anyways, I ended up using MaciejGórski's lib, [Android Maps Extensions](http://code.google.com/p/android-maps-extensions/) so I'd pretty much give it a go if I were you – h4lc0n Jan 06 '14 at 19:17

2 Answers2

4

You would be better off using Handler. This doesn't create additional threads, which are not necessary.

Just call this in onCameraChange:

handler.removeMessages(MSG_ID);
handler.sendEmptyMessageDelayed(MSG_ID, 2000);

and in handleMessage do your work.

Why would you need it dynamic if you show only several markers? Markers outside visible region are not making is much slower. Your code that clears and adds markers may make it slower.

If you were to show thousands of markers, try Android Maps Extensions, which has adding only visible markers built in.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • 1
    Very reasonable and thought out solution, I can vouch that this works for a regular use case such as moving a marker around the map and when it settles posting the location where the marker landed in a textbox above or something like that. Before this it will freeze because the code in onCameraChange runs every single time there is a change. – Odaym Apr 08 '15 at 07:08
0

I don't know if you keep needing a good solution, but I found this question and think could help. It help me, the guy wrote a blog post explaining how to use cluster manager and how to set up.

Community
  • 1
  • 1
Sierisimo
  • 639
  • 8
  • 21