使用轴承的车辆运动

时间:2018-04-09 12:33:06

标签: android google-maps google-polyline bearing

目前正致力于车辆跟踪应用程序,我将必须显示汽车在折线上的运动,该折线是从当前位置到目的地设置的,并且在汽车的运动中我将不得不擦除像Uber / Ola这样的坐标

我现在面临两个问题,即

a>汽车在折线上完美移动但有时汽车在几个坐标处自行旋转可能是由于轴承错误。我该怎样处理这个问题?

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

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

update polyline according to the user moving android googleMaps 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;
    }

}

2 个答案:

答案 0 :(得分:0)

您可以从以下两个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;
}

答案 1 :(得分:0)

a&gt;汽车在折线上完美移动但有时汽车在几个坐标处自行旋转可能是由于轴承错误。我该怎样处理这个问题?

你可能正在使用MockLocations或一些类似的模拟GPS应用来测试它,对吧?因为,至少对于Mock Locations,它会在停止时执行。在真实环境中尝试。

b&gt;无法理解如何删除汽车已经过的坐标。

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

  2. 将列表中的所有元素循环到最近的点并删除它们。只是&#34;休息&#34;一旦找到它就会出现for循环。 请注意,在迭代其元素时,您无法从列表中删除元素。您必须创建一个新列表(类似于obsoletePoints),并在迭代它时将它们添加到该列表中。

  3. 然后从原始列表中删除.removeAll(obsoletePoints)。

  4. 像这样:

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

    希望它有所帮助。

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