Search code examples
androidgoogle-mapsfirebasefirebase-realtime-databasetracking

Animate or Move marker along with rout path on GoogleMap


First time i am working with google maps and i am stuck in very critical situation. Actually i want to animate marker along with poly line(rout drawn), in this regard i have been attempted many codes but not single one approach is going to fire.

What approach i am using that is first time i draw a rout b/w rider and user destinations, After Ward as the lat long of rider is changes i push his lat long to firebase. Meanwhile i am getting the update lat long of rider to user side in map and then rout draw.

enter image description here

For your better understanding here is my code.

Here is how i draw path b/w rider and user...

public void pathDraw(String startLat,String startLon,String endLat,String endLon,int orginDrawable, int desTinationDrawable){

    LatLng origin = new LatLng(Double.parseDouble(startLat), Double.parseDouble(startLon));
    LatLng destination = new LatLng(Double.parseDouble(endLat), Double.parseDouble(endLon));
    DrawRouteMaps.getInstance(this)
            .draw(origin, destination, mGoogleMap);
    DrawMarker.getInstance(this).draw(mGoogleMap, origin,orginDrawable, "Origin Location");
    DrawMarker.getInstance(this).draw(mGoogleMap, destination, desTinationDrawable, "Destination Location");
    LatLngBounds bounds = new LatLngBounds.Builder()
            .include(origin)
            .include(destination).build();
    Point displaySize = new Point();
    getWindowManager().getDefaultDisplay().getSize(displaySize);
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, displaySize.x, 1000, 15));
}

And here is the Firebase method cal every time when lat long update.

  public void mapChangeAnimation(final boolean user){
    mDatebaseTracking.keepSynced(true);
    DatabaseReference query = mDatebaseTracking.child(order_id);

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            rider_lat = ""+dataSnapshot.child("rider_lat").getValue();
            rider_long = ""+dataSnapshot.child("rider_long").getValue();
            String latlong = rider_lat + " "+ rider_long;

            if(!user) {
                LatLng latLngBegin = new LatLng(Double.parseDouble(rider_lat),Double.parseDouble(rider_long));
                LatLng latLngEnd = new LatLng(Double.parseDouble(rest_lat),Double.parseDouble(rest_long));   
            }
            else {
                LatLng latLngBegin = new LatLng(Double.parseDouble(rider_lat),Double.parseDouble(rider_long));
                LatLng latLngEnd = new LatLng(Double.parseDouble(user_lat),Double.parseDouble(user_long));                  
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

You suggestion will be appreciated. Also if you please post a code according to my scenario. Thanks in advance


Solution

  • To animate your marker along the route path from starting point to an ending point, you can refer to this code.

    https://gist.github.com/broady/6314689

    If you want for easier implementation, you can use libraries available to do the same.

    1. https://github.com/amalChandran/trail-android
    2. https://github.com/amanjeetsingh150/UberCarAnimation

    The libraries do not have exact implementation and you may have to tweak it a bit.