2

我正在使用谷歌地图 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();
}
4

3 回答 3

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);
        }
    }
};

开始(或暂停后恢复)添加Markers:

handler.post(worker);

暂停:

handler.removeCallbacks(worker);

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

于 2013-10-07T17:01:09.493 回答
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();
}
于 2013-10-07T11:19:29.617 回答
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); //

}
于 2013-10-07T11:48:29.143 回答