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());
}
}
);
從哪裏獲得了「listpoint」折線? –
我們正在考慮listPoint有緯度和經度列表。 – Singh
沒錯。你能指定我應該把這個處理程序代碼放在哪裏嗎?像我內心運行功能或只是在按鈕內點擊? –