我正在Android应用中开发订单跟踪模块,在此模块中,我实施了一个源位置标记和目标位置标记,来源位置是固定的,目的地位置标记会随着位置的改变而不断变化,但是当位置发生变化时,标记从一个位置跳到另一个位置,它不能平滑移动, 实际上我正在删除prev标记并在新位置创建新标记, 有没有办法顺利移动标记或我可以更新以前的标记位置?请帮我。
这是代码段。
public void updatePackageLoc(){
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
while(updatelocation) {
Track_Location();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
if (package_marker != null && package_newlat != 0.0 && package_newlon != 0.0) {
package_marker.remove();
LatLng position = new LatLng(package_newlat, package_newlon);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,14f));
package_marker = googleMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromResource(R.drawable.pacage)));
}
}
});
}
}
};
new Thread(runnable).start();
}
答案 0 :(得分:0)
这对我有用,声明finalPosition为全局变量并在Track_location函数中更新。
public void updatePackageLoc(){
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
while(updatelocation) {
Track_Location();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
long elapsed;
float t;
float v;
@Override
public void run() {
// Calculate progress using interpolator
elapsed = SystemClock.uptimeMillis() - start;
t = elapsed / durationInMs;
v = interpolator.getInterpolation(t);
// Log.d("order_track",startPosition.toString()+ " " +finalPosition.toString());
LatLng currentPosition = new LatLng(
startPosition.latitude*(1-t)+finalPosition.latitude*t,
startPosition.longitude*(1-t)+finalPosition.longitude*t);
package_marker.setPosition(currentPosition);
// Repeat till progress is complete.
if (t < 1) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}
}
};
new Thread(runnable).start();
}