Domanda

After long search on how to blink a marker i came up with this code in my project using google maps v2. Here is the code..

MyMarker = map.addMarker(new MarkerOptions().position(current_loc).title(address).snippet(city).
                        icon(BitmapDescriptorFactory.fromResource(R.drawable.bus4)));

                markertimer = new Timer();
                markertimer.scheduleAtFixedRate(new TimerTask() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                blinkMarker();
                            }
                        });
                    }
                }, 0, 500);

here the blinkMarker method..

private boolean blinkMarker(){


    if(marker == true){
        current.setVisible(true);
        marker = false;
        }
        else if(marker == false){
        current.setVisible(false);
        marker = true;
        }
    return marker;
}

So the above is working fine and the marker is blinking , but my doubt is

  1. will the above code use more memory for blinking?
  2. and i'm using reverse geocoding for getting address, so not able to click the marker when blinking. Please suggest any other way to blink the marker?
È stato utile?

Soluzione

Try replacing setVisible with setIcon. When doing that you need to prepare another drawable for icon with the same size as bus4.png, but with all pixels transparent.

Note there is a bug related to setIcon that causes info window to disappear.

As a side note a couple of problems in your code:

  1. Using Timer is wasteful. It uses Thread, which you don't need. Consider using Handler instead.
  2. You (probably) have a memory leak. Timer will not stop working when the Activity is destroyed. You need to cancel it if you don't do that already. If switching to Handler, rember to remove any Runnables or Messages you send.

Altri suggerimenti

Usually a Google map is more busy: with many markers. So, you can use Scheduledthreadpoolexecutor with runnable.

Your blinkMarket method could be shorter, like this: 'current.setVisible(!marker)'. I'm affraid that using Timer is not right approach. You can do the same using Handler with postDelayed. If the flashing of marker depends on some condition, that should be repeately checked, than flashing task should not be generated twice.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top