Android 在andriod中的google地图上每隔10秒绘制一条多段线

Android 在andriod中的google地图上每隔10秒绘制一条多段线,android,google-maps,gps,location,polyline,Android,Google Maps,Gps,Location,Polyline,我制作了一个应用程序,在这个应用程序中,用户的位置在按下开始按钮后每隔10秒被提取一次,并且从旧位置到新位置绘制一条多段线,直到按下停止按钮为止。 现在的问题是,当多段线代码位于run函数中时,地图上没有绘制任何线。但是,如果我把代码放在run函数之外,它工作得很好(比如当我再次按下start按钮时,我得到了多段线),但我不想每次都按这个按钮,我想通过计时器自己画一条线 这是我的密码 track_record.setOnClickListener(new View.OnClickList

我制作了一个应用程序,在这个应用程序中,用户的位置在按下开始按钮后每隔10秒被提取一次,并且从旧位置到新位置绘制一条多段线,直到按下停止按钮为止。 现在的问题是,当多段线代码位于run函数中时,地图上没有绘制任何线。但是,如果我把代码放在run函数之外,它工作得很好(比如当我再次按下start按钮时,我得到了多段线),但我不想每次都按这个按钮,我想通过计时器自己画一条线

这是我的密码

    track_record.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Toast.makeText(getApplication(), "Your Tracking is started now", Toast.LENGTH_SHORT).show();
                                                ///////*************************************////////
                                                // create class object
                                                gps = new GPSTracker(MapsActivity.this);
                                                timer.scheduleAtFixedRate(new TimerTask() {

                                                    @SuppressLint("DefaultLocale")
                                                    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
                                                    @Override

                                                    public void run() {
                                                        runOnUiThread(new Runnable() {
                                                            @Override
                                                            public void run() {

                                                                LatLng current =   new LatLng(latitude = gps.getLatitude(),longitude = gps.getLongitude());

                                                                if (begin == 0) {
                                                               fixedBegin = current;

                                                                    // create marker
                                                                    MarkerOptions marker = new MarkerOptions().position(fixedBegin).title("Begin ");

                                                                    // Changing the color babyyy
                                                                  marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
                                                                    // adding marker
                                                                mMap.addMarker(marker);


                                                                    // Not working here, but should be

                                                                    if(Flag==0)  //when the first update comes, we have no previous points,hence this
                                                                    {
                                                                        prev=current;
                                                                        Flag=1;
                                                                    }
                                                                    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(current, 16);
                                                                    mMap.animateCamera(update);
                                                                    mMap.addPolyline((new PolylineOptions())
                                                                            .add(prev, current).width(6).color(Color.BLUE)
                                                                            .visible(true));
                                                                    prev=current;
                                                                    current = null;

                                                              }
                                                                begin++;

                                                                Log.i("OK", "lat------ " + latitude);
                                                                Log.i("OK", "lng-------- " + longitude);

                                                                arrLat.add(latitude);
                                                                arrLng.add(longitude);

                                                                //////////// TRYING ///////////
                                                                // And it Worked here

/*
                                                                if(Flag==0)  //when the first update comes, we have no previous points,hence this
                                                                {
                                                                    prev=current;
                                                                    Flag=1;
                                                                }
                                                                CameraUpdate update = CameraUpdateFactory.newLatLngZoom(current, 16);
                                                                mMap.animateCamera(update);
                                                                mMap.addPolyline((new PolylineOptions())
                                                                        .add(prev, current).width(6).color(Color.BLUE)
                                                                        .visible(true));
                                                                prev=current;
                                                                current = null;
*/


                                                            }
                                                        });


                                                    }
                                                }, 0, TIME_INTERVAL);


                                                // check if GPS enabled
                                                if (gps.canGetLocation()) {

                                                    latitude = gps.getLatitude();
                                                    longitude = gps.getLongitude();
                                                    String longlat = String.valueOf(latitude) + ":" + String.valueOf(longitude);
                                                    cordsList.add(longlat);
                                                    // \n is for new line
                                                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                                                } else {
                                                    Toast.makeText(getApplicationContext(), "Sorry cant get location", Toast.LENGTH_LONG).show();
                                                    // can't get location
                                                    // GPS or Network is not enabled
                                                    // Ask user to enable GPS/network in settings
                                                    // gps.showSettingsAlert();
                                                }

                                                Log.i("Finall", "Location-> " + cordsList.toString());

                                            }
                                        }
        );

为此,可以使用处理程序

类变量

Handler m_handler;
Runnable m_handlerTask ;
int t=0;
使用处理程序以10秒的延迟绘制多段线,使用lat和long绘制多段线

m_handler = new Handler();
m_handlerTask = new Runnable()
{
@Override 
public void run() { 
if(t<listPoint.size()-1)
{     
LatLng src = listPoint.get(t);
LatLng dest = listPoint.get(t + 1);
Polyline line = mMap.addPolyline(new PolylineOptions()
    .add(new LatLng(src.latitude, src.longitude),
    new LatLng(dest.latitude,dest.longitude))                                       
    .width(2).color(Color.BLUE).geodesic(true)); 
    t++;
    }
    else
    {
   m_handler.removeCallbacks(m_handlerTask);
    } 
   m_handler.postDelayed(m_handlerTask, 10000);    
    }
};
m_handlerTask.run(); 
m_handler=new handler();
m_handlerTask=new Runnable()
{
@凌驾
public void run(){

如果(tWe)考虑listPoint有纬度和经度的列表。对。你能指定我应该把这个处理程序代码放在哪里吗?比如在我运行函数的内部还是在按钮点击的内部?