Brandon Zamudio
Brandon Zamudio

Reputation: 2873

Ways to animate a path by placing markers

I'm trying to perform an animation to simulate a route on a map, I have to show a path with markers, putting one by one, waiting X time before puts the next. First, I used threads to wait to put the next marker, but it doesn't run on any devices and makes interruptions on the map while running, so it makes the user experience so bad. Then I decided to use the callback feature of animateCamera (Android API reference here) to avoid interruptions, but I can't find some way to make an animation after each other, the number of markers and locations are undetermined, so I have to iterate. Here's some of the last idea code:

hashMarkers.get("Initial").setVisible(true);
        mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 5000, new GoogleMap.CancelableCallback() {
            @Override
            public void onFinish() {
                for (COUNT=1; COUNT < hashMarkers.size() - 1;){
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(hashMarkers.get(String.valueOf(COUNT)).getPosition())   // Sets the center of the map to
                            .zoom(16)                   // Sets the zoom
                            .bearing(-bearing)                // Sets the orientation of the camera to east
                            .tilt(90)                   // Sets the tilt of the camera to 30 degrees
                            .build();                   // Creates a CameraPosition from the builder
                    hashMarkers.get(String.valueOf(COUNT)).setVisible(true);
                    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 3000, new GoogleMap.CancelableCallback() {
                        @Override
                        public void onFinish() {
                            COUNT++;
                        }

                        @Override
                        public void onCancel() {

                        }
                    });

This code only shows the initial marker and the first one in the hashmap.

Thank you very much for any contribution that may help solve this problem!

Upvotes: 1

Views: 90

Answers (1)

Brandon Zamudio
Brandon Zamudio

Reputation: 2873

I found the solution, create apart the cancelable callback it works for me and could be helpful to others.

mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 5000, new GoogleMap.CancelableCallback() {
        @Override
        public void onFinish() {
            mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 3000, myCallBack);
        }

        @Override
        public void onCancel() {

        }
    });
}

GoogleMap.CancelableCallback myCallBack = new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
    if(++COUNT < hashMarkers.size()){

        CameraPosition cameraPosition =
                new CameraPosition.Builder()
                        .target(hashMarkers.get(String.valueOf(COUNT)).getPosition())
                        .tilt(90)
                        .bearing(-bearing)
                        .zoom(16)
                        .build();

        hashMarkers.get(String.valueOf(COUNT)).setVisible(true);

        mMap.animateCamera(
                CameraUpdateFactory.newCameraPosition(cameraPosition),
                3000,
                COUNT == hashMarkers.size() - 1 ? FinalCancelableCallback : myCallBack);
    }
}

@Override
public void onCancel() {
}
};

GoogleMap.CancelableCallback FinalCancelableCallback = new GoogleMap.CancelableCallback() {

@Override
public void onFinish() {

    CameraPosition cameraPosition2 = new CameraPosition.Builder()
            .target(hashMarkers.get("Final").getPosition())   // Sets the center of the map to
            .zoom(17)                   // Sets the zoom
            .bearing(-bearing)                // Sets the orientation of the camera to east
            .tilt(90)                   // Sets the tilt of the camera to 30 degrees
            .build();                   // Creates a CameraPosition from the builder
    hashMarkers.get("Final").setVisible(true);
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition2), 3000, new GoogleMap.CancelableCallback() {
        @Override
        public void onFinish() {
            allView = new LatLngBounds(
                    allLatLng.get(0), allLatLng.get(allLatLng.size() - 1));
            mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(allView, 80), 2000, null);
        }

        @Override
        public void onCancel() {

        }
    });

}

@Override
public void onCancel() {
}

};

Upvotes: 1

Related Questions