gpt4 book ai didi

android - 在 android 的谷歌地图上每 10 秒绘制一条折线

转载 作者:行者123 更新时间:2023-11-30 00:59:14 24 4
gpt4 key购买 nike

我制作了一个应用程序,一旦按下开始按钮,每 10 秒就会获取用户的位置,并从旧位置到新位置绘制多段线,直到按下停止按钮。现在的问题是当折线代码在运行函数中时, map 上没有画线。但是如果我将该代码放在运行函数之外,它工作正常(就像当我重新按下开始按钮时我得到折线)但我不想每次都按下按钮,我希望那条线是通过计时器自行绘制。

这是我的代码

    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 秒的处理程序来使用经纬度绘制折线

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

关于android - 在 android 的谷歌地图上每 10 秒绘制一条折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39688563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com