用计时器将标记放在地图上

时间:2013-10-07 11:15:38

标签: android google-maps timer google-maps-markers timertask

我正在使用谷歌地图v2在地图上添加标记(大约200个)作为路径,并完美地放置在地图上。 但我的要求是添加一些时间间隔的每个标记,所以我希望它看起来像一些动画。所以我尝试了两种方法, 第一种方法是

latvalues[] = {....};
longvalues[]={....};
for(int i=0;i<latvalues.length;i++){
   map.addMarker(new MarkerOptions().position(new LatLng(latvalues[i],longvalues[i])).title("Info")
                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.img)));
Thread.sleep(500);
}

这不起作用,只有在放置所有标记后,地图才会加载..

第二种方法是

 latvalues[] = {....};
longvalues[]={....};
Timer marktimer = new Timer();
markTimer.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {

    for(int i=0;i<latvalues.length;i++){
   map.addMarker(new MarkerOptions().position(new LatLng(latvalues[i],longvalues[i])).title("Info")
                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.img)));
    }
}
},0, 500);

marktimer.cancel();

这甚至没有加载任何标记......

请建议任何解决方法来实现这一目标......

使用倒数计时器编辑的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.datemap);

    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.busmap);
    map=fm.getMap();
    map.setMyLocationEnabled(true);

    countdown();
}
public void countdown(){
    if(this.ctimer != null){
        this.ctimer.cancel();
    }
    this.ctimer = new CountDownTimer(1000,500) {

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            if(markerno<latitudeValues1.length){
                map.addMarker(new MarkerOptions().position(new LatLng(latitudeValues1[markerno],longitudevalues1[markerno])).title("Info")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
                markerno++;
            }
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitudeValues1[markerno],longitudevalues1[markerno]), 12));
        }
    }.start();
}

3 个答案:

答案 0 :(得分:1)

private final Handler handler = new Handler();
private final Runnable worker = new Runnable() {
    private int i = 0;
    @Override
    public void run() {
        // add marker with index i
        i++;
        if (i < latvalues.length) {
            handler.postDelayed(this, 500);
        }
    }
};

开始(或暂停后继续)将Marker添加到:

handler.post(worker);

暂停:

handler.removeCallbacks(worker);

注意:与往常一样,您需要在代码中调用removeCallbacks以避免泄露Activity

答案 1 :(得分:0)

您是否尝试过使用CountdownTimer?

int markerNo = 0;
public void startCountDown(){       
    if(this._countDownTimer != null){
        this._countDownTimer.cancel();
    }
    this._countDownTimer = new CountDownTimer(TIME_FOR_SET_IN_S * 1000, 500){

        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
                            if(markerNo < longvalues.size()){
                             map.addMarker(new MarkerOptions().position(new LatLng(latvalues[markerNo],longvalues[markerNo])).title("Info")
                                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.img)));
                              markerNo++
                            }

        }

    }.start();
}

答案 2 :(得分:0)

试试这个

final Handler _han = new Handler();
for(int i = 1;i<=50;i++)
{
_han.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    yourMethodToAddMarker();
                    Thread.sleep(1000);
                }
            }, 2000); //

}