2016-09-25 90 views
1

我已經制作了一個應用程序,用戶的位置在每10秒鐘被提取一次,一旦開始按鈕被按下,並且一條折線從舊到新的位置被繪製直到停止按鈕是按下。 現在的問題是,折線代碼在運行功能內部時,地圖上沒有繪製線條。但如果我把代碼放在運行函數之外,它工作正常(就像當我重新按下開始按鈕時我得到折線),但我不想每按一次按鈕,我都希望該線是通過計時器自行繪製。在谷歌地圖上每隔10秒後繪製一條折線在andriod

這裏是我的代碼

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

              } 
             } 
     ); 

回答

1

你可以使用一個處理程序用於此目的。

類變量。

Handler m_handler; 
Runnable m_handlerTask ; 
int t=0; 

使用處理,以10秒的延遲繪製使用lat和長

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(); 
+0

從哪裏獲得了「listpoint」折線? –

+0

我們正在考慮listPoint有緯度和經度列表。 – Singh

+0

沒錯。你能指定我應該把這個處理程序代碼放在哪裏嗎?像我內心運行功能或只是在按鈕內點擊? –