当服务器上的驱动程序位置(lat lng)发生变化时,将标记设置为新位置(如超级汽车动画)的动画

时间:2018-05-15 05:56:00

标签: android animation

private void GetDriverloc(HashMap<String, String> map) {
    Call<DriverLocationResToCus> call = apiInterface.GetDriverLoc(map);
    System.out.println("enter the currency alert api" + call.request().url());
    call.enqueue(new Callback<DriverLocationResToCus>() {
        @Override
        public void onResponse(Call<DriverLocationResToCus> call, Response<DriverLocationResToCus> response) {


            if (response.isSuccessful()) {

                assert response.body() != null;
                DriverStartLat = response.body().getDriverCurrentLatStart();
                DriverStartLng = response.body().getDriverCurrentLngStart();
                DriverEndLat = response.body().getDriverCurrentLatEnd();
                DriverEndLng = response.body().getDriverCurrentLngEnd();

/这里我从服务器获取驱动程序的位置。我需要将驱动程序lat lnt传递给循环并相应地为标记设置动画。我试图将它直接传递给startpostion和endpostion。但标记仍在不断闪烁。

                Utilities.printV("DriverStartLat", DriverStartLat);

                ValueAnimator polylineAnimator = ValueAnimator.ofInt(0, 100);
                polylineAnimator.setDuration(2000);
                polylineAnimator.setInterpolator(new LinearInterpolator());
                polylineAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        List<LatLng> points = greyPolyLine.getPoints();
                        int percentValue = (int) valueAnimator.getAnimatedValue();
                        int size = points.size();
                        int newPoints = (int) (size * (percentValue / 100.0f));
                        List<LatLng> p = points.subList(0, newPoints);
                        blackPolyline.setPoints(p);
                    }
                });
                polylineAnimator.start();



                    mHandler = new Handler();
                    index = -1;
                    next = 1;
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {

                        if (index < points.size() - 1) {
                            index++;
                            next = index + 1;
                        }
                        if (index < points.size() - 1) {
                            startPosition = points.get(index);
                            endPosition = points.get(next);
                        }

//当我将驱动程序的lat lng直接传递给startposition和endposition时,标记移动到新位置,但如果它没有从服务器获得新的lat lat并且也连续闪烁,则会返回。

                            ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
                            valueAnimator.setDuration(3000);
                            valueAnimator.setInterpolator(new LinearInterpolator());
                            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                                @Override
                                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                                    v = valueAnimator.getAnimatedFraction();
                                    lng = v * endPosition.longitude + (1 - v)
                                            * startPosition.longitude;
                                    lat = v * endPosition.latitude + (1 - v)
                                            * startPosition.latitude;

                                    LatLng newPos = new LatLng(lat, lng);
                                    marker.setPosition(newPos);
                                    marker.setAnchor(0.5f, 0.5f);
                                    marker.setRotation(getBearing(startPosition, newPos));
                        mMap.moveCamera(CameraUpdateFactory
                                .newCameraPosition
                                        (new CameraPosition.Builder()
                                                .target(newPos)
                                                .zoom(15.5f)
                                                .build()));
                                }
                            });
                            valueAnimator.start();
                            mHandler.postDelayed(this, 3000);
                        }
                    }, 3000);

                Utilities.printV("DriverLocationResToCus", "DriverLocationResToCus SUCCESS");


            } else {

                Utilities.printV("DriverLocationResToCus", "DriverLocationResToCus FAILURE");

//任何人都可以帮我解决这个问题

            }
        }

        @Override
        public void onFailure(Call<DriverLocationResToCus> call, Throwable t) {

        }
    });

}

2 个答案:

答案 0 :(得分:0)

@aravind工作正常!但是,我可以将其用于两个或三个驱动程序吗? 因为,我将代码编辑为具有多个驱动程序,下面的代码却出现了随机行为。

private ArrayMap<String, Marker> markerArrayMap = new ArrayMap<>();
if (!markerArrayMap.containsKey(driverId)) {
    markerArrayMap.put(driverId, mMap.addMarker(new MarkerOptions().position(latLngs.get(1))
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car))));
    MarkerAnimation.animateMarkerToGB(markerArrayMap.get(driverId), latLngs.get(1), new LatLngInterpolator.Spherical());
    markerArrayMap.get(driverId).setRotation(getBearing(latLngs.get(1), latLngs.get(0)));
} else {
    MarkerAnimation.animateMarkerToICS(markerArrayMap.get(driverId), latLngs.get(1), new LatLngInterpolator.Spherical());
    markerArrayMap.get(driverId).setRotation(getBearing(latLngs.get(1), latLngs.get(0)));
}

答案 1 :(得分:-1)

private float getBearing(LatLng begin, LatLng end) {
    double lat = Math.abs(begin.latitude - end.latitude);
    double lng = Math.abs(begin.longitude - end.longitude);

    if (begin.latitude < end.latitude && begin.longitude < end.longitude)
        return (float) (Math.toDegrees(Math.atan(lng / lat)));
    else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
        return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
    else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
        return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
    else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
        return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
    return -1;
}