使用轴承的车辆运动



目前正在开发车辆跟踪应用程序,我将不得不在从当前位置到目的地设置的折线上显示汽车的运动,并且在汽车的运动上,我将不得不像Uber/Ola一样擦除坐标。

我现在面临两个问题,即

答>汽车在折线上完美移动,但有时汽车在几个坐标上自行旋转可能是由于错误的方位。如何处理此问题?

b>无法理解如何删除汽车经过的坐标。

我在下面有这个链接,可以在移动时更新折线,但无法理解我将在哪里放置代码以使其工作。

根据用户移动安卓谷歌地图v2更新折线

我尝试过的代码。

https://github.com/avik1990/PolylineUpdater

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
List<LatLng> list_coordianates = new ArrayList();
PolylineOptions polyLineOptions = null;
private TimerTask NoticeTimerTask;
private final Handler handler = new Handler();
Timer timer = new Timer();
int index = 0, start_flag = 0;
boolean forward = false;
boolean isMarkerRotating;
Marker mMarker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(22.573764, 88.359109);
mMarker = mMap.addMarker(new MarkerOptions().position(sydney).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car)).anchor(0.5f, 0.5f));
mMarker.setAnchor(0.5f, 0.5f);
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 18.0f));
fillpath();
}
///hardcoded coordinated collected from google map
private void fillpath() {
list_coordianates.clear();
list_coordianates.add(new LatLng(22.573764, 88.359109));
list_coordianates.add(new LatLng(22.573947, 88.359174));
list_coordianates.add(new LatLng(22.574145, 88.359244));
list_coordianates.add(new LatLng(22.574328, 88.359313));
list_coordianates.add(new LatLng(22.574526, 88.359372));
list_coordianates.add(new LatLng(22.574720, 88.359469));
list_coordianates.add(new LatLng(22.574918, 88.359506));
list_coordianates.add(new LatLng(22.575111, 88.359571));
list_coordianates.add(new LatLng(22.575299, 88.359624));
list_coordianates.add(new LatLng(22.575365, 88.359370));
list_coordianates.add(new LatLng(22.575380, 88.359329));
list_coordianates.add(new LatLng(22.575390, 88.359276));
list_coordianates.add(new LatLng(22.575407, 88.359230));
list_coordianates.add(new LatLng(22.575432, 88.359134));
list_coordianates.add(new LatLng(22.575457, 88.359083));
list_coordianates.add(new LatLng(22.575472, 88.359024));
list_coordianates.add(new LatLng(22.575459, 88.358966));
list_coordianates.add(new LatLng(22.575511, 88.358903));
list_coordianates.add(new LatLng(22.575516, 88.358871));
list_coordianates.add(new LatLng(22.575541, 88.358820));
list_coordianates.add(new LatLng(22.575546, 88.358755));
list_coordianates.add(new LatLng(22.575556, 88.358710));
list_coordianates.add(new LatLng(22.575596, 88.358629));
list_coordianates.add(new LatLng(22.575620, 88.358576));
list_coordianates.add(new LatLng(22.575591, 88.358512));
list_coordianates.add(new LatLng(22.575626, 88.358481));
list_coordianates.add(new LatLng(22.575634, 88.358443));
list_coordianates.add(new LatLng(22.575642, 88.358415));
list_coordianates.add(new LatLng(22.575660, 88.358388));
list_coordianates.add(new LatLng(22.575663, 88.358355));
list_coordianates.add(new LatLng(22.575684, 88.358316));
list_coordianates.add(new LatLng(22.575637, 88.358343));
list_coordianates.add(new LatLng(22.575613, 88.358355));
list_coordianates.add(new LatLng(22.575588, 88.358357));
list_coordianates.add(new LatLng(22.575577, 88.358329));
list_coordianates.add(new LatLng(22.575551, 88.358336));
list_coordianates.add(new LatLng(22.575531, 88.358341));
list_coordianates.add(new LatLng(22.575507, 88.358349));
list_coordianates.add(new LatLng(22.575480, 88.358355));
list_coordianates.add(new LatLng(22.575463, 88.358359));
list_coordianates.add(new LatLng(22.575439, 88.358361));
list_coordianates.add(new LatLng(22.575422, 88.358363));
list_coordianates.add(new LatLng(22.575401, 88.358365));
list_coordianates.add(new LatLng(22.575381, 88.358364));
list_coordianates.add(new LatLng(22.575367, 88.358352));
list_coordianates.add(new LatLng(22.575287, 88.358345));
list_coordianates.add(new LatLng(22.575223, 88.358323));
list_coordianates.add(new LatLng(22.575144, 88.358323));
list_coordianates.add(new LatLng(22.575094, 88.358355));
list_coordianates.add(new LatLng(22.575005, 88.358339));
list_coordianates.add(new LatLng(22.574881, 88.358414));
list_coordianates.add(new LatLng(22.574861, 88.358506));
list_coordianates.add(new LatLng(22.574866, 88.358613));
list_coordianates.add(new LatLng(22.574836, 88.358677));
list_coordianates.add(new LatLng(22.574836, 88.358784));
list_coordianates.add(new LatLng(22.574817, 88.358843));
list_coordianates.add(new LatLng(22.574827, 88.359010));
list_coordianates.add(new LatLng(22.574846, 88.359144));
list_coordianates.add(new LatLng(22.574802, 88.359273));
list_coordianates.add(new LatLng(22.574802, 88.359337));
list_coordianates.add(new LatLng(22.574777, 88.359417));
list_coordianates.add(new LatLng(22.574861, 88.359487));
list_coordianates.add(new LatLng(22.574945, 88.359535));
list_coordianates.add(new LatLng(22.575049, 88.359552));
list_coordianates.add(new LatLng(22.575248, 88.359605));
///for drawing the polyline
drawpolyline();
}
private void drawpolyline() {
polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(list_coordianates);
polyLineOptions.width(10);
polyLineOptions.color(Color.RED);
mMap.addPolyline(polyLineOptions);
///for car movement
movecaronpath();
}
///used timer to dalay the index
private void movecaronpath() {
timer = new Timer();
NoticeTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
/////////////////////////////////////////
makeAni2(list_coordianates.get(index));
/////////////////////////////////////////
if (forward) {
index++;
} else {
index--;
Log.d("Uber..... ", index + "");
if (index < 0) {
forward = true;
index = 0;
}
}
if (index == list_coordianates.size() && forward) {
index--;
forward = false;
} else {
//                            forward=true;
}
}
});
}
};
timer.schedule(NoticeTimerTask, 0, 5000); 
}
private void makeAni2(final LatLng latlngg) {
final LatLng startPosition = mMarker.getPosition();
final LatLng finalPosition = latlngg;
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final Interpolator interpolator = new AccelerateDecelerateInterpolator();
final float durationInMs = 4000;
final boolean hideMarker = false;
///////for rotation of the marker
rotateMarker(mMarker, startPosition, finalPosition);
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;
LatLng currentPosition = new LatLng(
startPosition.latitude * (1 - t) + finalPosition.latitude * t,
startPosition.longitude * (1 - t) + finalPosition.longitude * t);
mMarker.setPosition(currentPosition);
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, 18));
// Repeat till progress is complete.
if (t < 1) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
mMarker.setVisible(false);
} else {
mMarker.setVisible(true);
}
}
}
});
}
private void rotateMarker(final Marker marker, LatLng startPos, LatLng stopPos) {
final double toRotation = bearingBetweenLocations(startPos, stopPos);
DecimalFormat decimalFormat = new DecimalFormat("#");
System.out.println(decimalFormat.format(toRotation));
if (!isMarkerRotating) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final float startRotation = marker.getRotation();
final long duration = 2000;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
isMarkerRotating = true;
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);
float rot = t * (float) toRotation + (1 - t) * startRotation;
marker.setRotation(-rot > 180 ? rot / 2 : rot);
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
isMarkerRotating = false;
}
}
});
}
}
////for calculating of the bearing
private double bearingBetweenLocations(LatLng latLng1, LatLng latLng2) {
double PI = 3.14159;
double lat1 = latLng1.latitude * PI / 180;
double long1 = latLng1.longitude * PI / 180;
double lat2 = latLng2.latitude * PI / 180;
double long2 = latLng2.longitude * PI / 180;
double dLon = (long2 - long1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
double brng = Math.atan2(y, x);
brng = Math.toDegrees(brng);
brng = (brng + 360) % 360;
return brng;
}
}

您可以从两个Location获得方位,如下所示的方法

public float getBearing(Location begin, Location end) {
float bearing = 0;
if (begin != null && end != null) {
bearing = begin.bearingTo(end);
}
AppLog.Log("BEARING", String.valueOf(bearing));
return bearing;
}

a>汽车在折线上完美移动,但有时汽车在几个坐标上自行旋转可能是由于错误的方位。如何处理此问题?

您可能正在使用模拟位置或一些类似的模拟GPS应用程序来测试它,对吗?因为,至少对于模拟位置,它会在停止时这样做。在真实环境中尝试一下。

b>无法理解如何删除汽车经过的坐标。

  1. 在列表中找到最接近汽车位置的点。

  2. 遍历列表中的所有元素,直到最近的点并删除它们。找到 for 循环后,只需"中断"它。 请注意,在循环访问元素时,无法从列表中删除元素。您必须创建一个新列表(类似于 obsoletePoints),并在循环访问时将它们添加到该列表中。

  3. 然后只需从原始列表中删除所有(过时点)。

这样:

for (LatLng currentLatLng : list_coordianate) {
if (currentLatLng.equals(closestLatLng)) {
break;
}
obsoletePoints.add(currentLatLng);
}
list_coordinate.removeAll(obsoletePoints);

希望对您有所帮助。

让我知道它是如何工作的,或者如果您有任何问题。

相关内容

  • 没有找到相关文章

最新更新