2

i am creating google map with multiple markers.Now i want to move a car icon between that markers.Using this Move markers in google map v2 android

I can move a car icon between from first point to second point.But it is not moving from second to third.When i used for loop for this it strightly goes to final point.I added delay also but nothing else worked for me.

Thanks in advance.

This is my code::

public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) {

        anim_map = myMap;


        anim_marker = myMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon))
                .position(directionPoint.get(0))
                .flat(true));


        myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));

        if (directionPoint.size() >= 2) {

            for (int i = 0; i < directionPoint.size() - 1; i++) {
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        animateMarker(anim_map, anim_marker, directionPoint, false);



                    }
                });


            }

        }


    }

private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
                               final boolean hideMarker) {

        final long start = SystemClock.uptimeMillis();
        final long duration = 350000;
        final Interpolator interpolator = new LinearInterpolator();


        h.post(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);

                //  Log.e("T Location", t + "");


                double lng = t * directionPoint.get(i + 1).longitude + (1 - t) * directionPoint.get(i).longitude;
                double lat = t * directionPoint.get(i + 1).latitude + (1 - t) * directionPoint.get(i).latitude;
                marker.setPosition(new LatLng(lat, lng));

                if (t < 1.0) {
                    h.postDelayed(this, 1);
                } else {
                    if (hideMarker) {
                        marker.setVisible(false);
                    } else {
                        marker.setVisible(true);
                    }
                }


            }
        });


    }
Community
  • 1
  • 1
Punithapriya
  • 131
  • 9

2 Answers2

0

You can go try this way to make marker move in GoogleMap similar to live driving direction tracking

    private Marker mCurrentMarker;
    private float ZOOMLEVEL=18.0f;
    private LatLng previousLatLon;
    private Handler mLocalHandler;
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLocalHandler = new Handler();
        previousLatLon=new LatLng(45.320372, 2.460161);
        //Initialize Marker once Google Map object is created
        mMarkerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker_icon));
        mMarkerOptions.position(previousLatLon);
        mCurrentMarker = mGoogleMap.addMarker(mMarkerOptions);


    }

    /**
     * Call this method to move marker in map to new location in map with updated location
     * @param marker
     * @param toPosition
     * @param fromPosition
     */
    public void animateMarker(final Marker marker, final LatLng toPosition,final LatLng fromPosition) {


        final long duration = 500;
        final Interpolator interpolator = new LinearInterpolator();

        mLocalHandler.post(new Runnable() {
            @Override
            public void run() {
                long elapsed = SystemClock.uptimeMillis() - mStartTime;
                float t = interpolator.getInterpolation((float) elapsed
                        / duration);
                marker.setPosition(toPosition);
                marker.setAnchor(Constants.MAPANCHOR, Constants.MAPANCHOR);
                mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, ZOOMLEVEL));
                if (t < 1.0) {
                    // Post again 16ms later.
                    mLocalHandler.postDelayed(this, 16);
                } else {
                    marker.setVisible(true);
                }
                }
            }
        });
        previousLatLon=toPosition;// reassign the previous location to current location
    }

Go through my answer for more details here

In your case put a sleep for some milliseconds after for loop

for (int i = 0; i < directionPoint.size() - 1; i++) {
try {
 Thread.sleep(5000);
} catch (InterruptedException e) 
{
 e.printStackTrace();
}
// rest of your code
Community
  • 1
  • 1
Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • I can get the correct positions.If i increment the i value it will move to the destination point.But my problem is, this is not moving slowly when i inc i value.When i open the app from first time itself the loop finished.I cant see that. – Punithapriya Jul 20 '16 at 07:12
  • As far as I understood, If you are hard-coding the values, in loop, you need to put delay . Example: 10 seconds. Then you will be able to see. – Sreehari Jul 20 '16 at 08:51
  • May be you can add the relevant code, without which its difficult to find the problem – Sreehari Jul 20 '16 at 09:24
  • after your for loop, put this try { Thread.sleep(8000); } catch (InterruptedException e) { e.printStackTrace(); } – Sreehari Jul 20 '16 at 09:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117793/discussion-between-punithapriya-and-stallion). – Punithapriya Jul 20 '16 at 09:41
0

Finally, i came up with this answer. We have to update all the values with respect to the size of arraylist.

  public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) {

        anim_map = myMap;


        anim_marker = myMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon))
                .position(directionPoint.get(0))
                .flat(true));


        myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));


        animateMarker(anim_map, anim_marker, directionPoint, false);


    }

    private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
                               final boolean hideMarker) {

        final long start = SystemClock.uptimeMillis();
        long duration = 3500;
        final Interpolator interpolator = new LinearInterpolator();

        float t = 0;

        for (int j = 0; j < directionPoint.size() - 1; j++) {
            Log.e("Location Value", j + " " + directionPoint.get(j).latitude + ", " + directionPoint.get(j).longitude + " : " + directionPoint.get(j + 1).latitude + ", " + directionPoint.get(j + 1).longitude);
            while (t < j + 1) {
                t = t - j;
                double lng = t * directionPoint.get(j + 1).longitude + (1 - t) * directionPoint.get(j).longitude;
                double lat = t * directionPoint.get(j + 1).latitude + (1 - t) * directionPoint.get(j).latitude;
                LatList.add(new LatLng(lat, lng));
                long elapsed = SystemClock.uptimeMillis() - start;
                t = interpolator.getInterpolation((float) elapsed / duration);
            }
        }
        Log.e("LatList value", LatList.size() + "");
        h.post(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                marker.setPosition(LatList.get(i));
                if ((LatList.size() - 1) > i)
                    h.postDelayed(this, 1);
                Log.e("I value", i + "");
                i++;
            }
        });


    }
Punithapriya
  • 131
  • 9