0

I'm implementing the animations for the group of cars(Markers) to rotate at the same time in google map v2.

so that i need to code my animation part inside handler.post() method.

By doing this the animation part(handler.post() method) is running for only one marker.

i have coded handler.post() method inside the for loop. It runs only for the first time which means only one marker is rotating. after that it is not working. My code is as follows.

private void animateCarsInMarkers(final MarkerOptions mark, final long bearing, final LatLng startPosition, final int position){

    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    final long duration = 3000;
    final Interpolator interpolator = new LinearInterpolator();
    final Marker marker = mGoogleMap.addMarker(mark);

    final float rotationValue = Float.parseFloat(String.valueOf(bearing));

    try {
        if(tempCarsArray != null && !tempCarsArray.isEmpty()){
            sLongitude = tempCarsArray.get(position).getDouble(LONGITUDE);
            sLatitude = tempCarsArray.get(position).getDouble(LATITUDE);
            sBearing = tempCarsArray.get(position).getLong("Bearing");

            final double dLongitude = startPosition.longitude;
            final double dLatitude = startPosition.latitude;
            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float time = interpolator.getInterpolation((float) elapsed / duration);
                    double lng = time * dLongitude + (1 - time) * sLongitude;
                    double lat = time * dLatitude + (1 - time) * sLatitude;
                    float rotationValue = time *  dbearing + (1-time) * sBearing;
                    marker.setRotation((-rotationValue > 180) ? (rotationValue / 2) : rotationValue);
                    marker.setPosition(new LatLng(lat, lng));
                    if (time < 1.0) {
                        handler.postDelayed(this, 16);
                    }
                }
            });
        tempCarsArray.clear();
    } else {

            marker.setPosition(startPosition);
            marker.setRotation(-rotationValue > 180 ? rotationValue / 2 : rotationValue);
        }
    }catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This is the method which i called inside for loop. But it runs only for the first time in loop. Later it is not working. So only one marker is animated among the group of markers. My for loop is as follows :

for(int i=0; i<size; i++){
animateCarsInMarkers(mark, bearing, latLng, i);
}

This loop runs only when the value of i=0 and it wont runs again. Thanks in advance.

Manikandan.S
  • 41
  • 1
  • 5

1 Answers1

0

If your 'for loop' ran only once, it means that your "for loop condition part", i<size for this instance, has already satisfied the condition. What I would suggest is for you to actually log "size" and check its value.

If you're looking for the size of an array, use tempCarsArray.length like:

for(int i=0; i < tempCarsArray.length; i++){
    animateCarsInMarkers(mark, bearing, latLng, i);
}

To check if size is causing the problem, try this.

If you know the actual number of markers you're expecting to rotate, try to substitute with an integer for now, like:

//if you're expecting 5 markers
for(int i=0; i < 5 ; i++){
     animateCarsInMarkers(mark, bearing, latLng, i);
}

If all markers did rotate, it means your size variable has only a value of 1.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Thanks for your solution. It works for me. I rectified my mistake, it is because of the size only. But still only one marker is animated. Could u pls suggest me any possible ways to **animate a group of markers at same time**. @newguy – Manikandan.S Jun 03 '16 at 07:45
  • Dont forget to accept or upvote the answer to indicate that it worked for you. Now, for the follow-up question here's an [SO thread](http://stackoverflow.com/questions/14864664/animating-markers-on-google-maps-v2) that might help. Video demo and code included. – ReyAnthonyRenacia Jun 03 '16 at 10:18
  • @Manikandan.S Did you get the solution for this? I am also working on same. I am not able to understand how to do this? can you please help me with that? – Yogesh Nikam Patil Apr 20 '19 at 05:58