0

I need to make animated movement if the airplane marker along stored coordinates in ArrayList collection. I've found the solution, how to do move marker from one coordinate to another. Here is the code of the method, which moves it:

private void animateMarker(final Marker plane,
                           final LatLng toPosition,
                           final boolean hideMarker,
                           int currentIndex) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = gMap.getProjection();
    Point startPoint = proj.toScreenLocation(plane.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    final long duration = 2500;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed / duration);
            double lat = t * toPosition.latitude + (1 - t) * startLatLng.latitude;
            double lng = t * toPosition.longitude + (1 - t) * startLatLng.longitude;
            plane.setPosition(new LatLng(lat, lng));

            if (t < 1.0) {
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    plane.setVisible(false);
                } else {
                    plane.setVisible(true);
                }
            }
        }
    });
    //Call the method which will start another animateMarker() 
    // after this thread is finished
    currentIndex++;
    if(currentIndex < theRoute.size()-1){
        movePlane(plane, currentIndex);
    }
}

This method works perfectly, when I'm trying to move the object from one coordinate to another, but I need to move it along the collection of coordinates. As you recognized, in the end of the code I increment the "currentIndex" value, and call movePlane method, which accepts the instance of the marker and new index value. Here the chaos starts, but let me show you the code:

private void movePlane(Marker plane, int index){
    if(currentCoordIndex < theRoute.size()){
        animateMarker(plane, theRoute.get(index), false, index);
    }
}

So, the plane marker is moving chaotically back and forth from coordinate to another and it is not acceptable. I suppose, that new thread is called, when the other is not finished. Maybe, solution is quite simple, but I really don't know how to do, what I expect.

Also i need to rotate the marker towards the next point, could you help me with that?

SanchelliosProg
  • 2,091
  • 3
  • 20
  • 32
  • Have you tried reading this SO ticket? (http://stackoverflow.com/questions/6800613/rotating-image-marker-image-on-google-map-v3). I found this sample app which demonstrate how to rotate using 'setRotation' : (http://stackoverflow.com/questions/6800613/rotating-image-marker-image-on-google-map-v3). I hope it might help you. – Android Enthusiast Jan 16 '16 at 17:48
  • Fortunately I've found the solution, I've moved the last if-conditional block to the if blocks of the run() method, and everything worked))) And thank you for your effort. – SanchelliosProg Jan 16 '16 at 19:06

0 Answers0