首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每隔10秒在andriod中的google地图上绘制一条折线

每隔10秒在andriod中的google地图上绘制一条折线
EN

Stack Overflow用户
提问于 2016-09-25 23:26:47
回答 1查看 700关注 0票数 1

我做了一个应用程序,按下start按钮后,每隔10秒获取用户的位置,并从旧位置绘制一条折线到新位置,直到按下stop按钮。现在的问题是,当折线代码在run函数中时,地图上没有绘制直线。但是如果我把代码放在run函数之外,它就能正常工作(就像当我重新按下start按钮时,我得到了polyline),但我不想每次都按下按钮,我想要通过计时器自动绘制这条线。

以下是我的代码

代码语言:javascript
运行
复制
    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());

                                            }
                                        }
        );
EN

回答 1

Stack Overflow用户

发布于 2016-09-25 23:33:02

您可以使用Handler来实现此目的。

类变量。

代码语言:javascript
运行
复制
Handler m_handler;
Runnable m_handlerTask ;
int t=0;

在延迟10秒的情况下使用处理程序绘制多段线

代码语言:javascript
运行
复制
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(); 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39688563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档