0

I am facing an issue right now I am developing an Android app similar to Uber or lyft but the problem that I am facing is the car is moving on the map smoothly but sometimes it is revolving at 360 degree at one position, I need to stop that please help, here is my animation code.

 private void animateMarkr(double laat, double lnng,Location prevLocation)
  {
    final LatLng toPosition=new LatLng(laat,lnng);
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = mGoogleMap.getProjection();
    Point startPoint = proj.toScreenLocation(carMarker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    final long duration = 3000;
    final boolean hideMarker=false;
    final Interpolator interpolator = new LinearInterpolator();
    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            LatLng pre=carMarker.getPosition();
            float t = interpolator.getInterpolation((float) elapsed / duration);
            double lng = t  toPosition.longitude + (1 - t)  startLatLng.longitude;
            double lat = t  toPosition.latitude + (1 - t)  startLatLng.latitude;
          //  carMarker.setRotation(getBearing(pre,new LatLng(lat,lng)));
            carMarker.setPosition(new LatLng(lat, lng));

            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 20);
            } else {

                if (hideMarker) {
                    carMarker.setVisible(false);
                } else {
                    carMarker.setVisible(true);
                }
            }
        }
    });
}

Here is my Rotation Code

private void rotateMarker(Location location,Location preLocation){
    Location prevLocation = new Location(LocationManager.GPS_PROVIDER);
    prevLocation.setLatitude(carMarker.getPosition().latitude);
    prevLocation.setLongitude(carMarker.getPosition().longitude);
    final float toRotation = prevLocation.bearingTo(location);
           performRotaion(toRotation);
  }

private void performRotaion(final float toRotation){
    if(!isMarkerRotating) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final float startRotation = carMarker.getRotation();
        final float totalDegree=0;
        final long duration = 1000;

        final Interpolator interpolator = new LinearInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {
                isMarkerRotating = true;

                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed / duration);

                float rot = t  toRotation + (1 - t)  startRotation;

                if(carMarker != null){
                    carMarker.setAnchor(0.5f, 0.5f);
                    // if(rot<0.0f  && rot>-355.0f) {
                    carMarker.setRotation(-rot > 180 ? rot / 2 : rot);
                    carMarker.setFlat(true);
                }
                if (t < 1.0) {
                    handler.postDelayed(this, 16);
                } else {
                    isMarkerRotating = false;
                }
            }
        });
    }
}

// method call

rotateMarker(location,previcsLocation);
animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);

I have implemented the check to stop rotation if the degree is 360 also I have checked if the degree is more than 180 degree than divide the same and move the car on that angle. But nothing works

Zoe
  • 27,060
  • 21
  • 118
  • 148
Vaib
  • 1
  • 1
  • I'm not sure on the reason/cause of that behavior of your marker, so what can I give you is another way on how to [animate the rotation of the marker](http://stackoverflow.com/questions/28967821/animate-the-rotation-of-the-marker-in-google-map-v2). I hope it can give you an idea on how to solve this problem. – KENdi Sep 08 '16 at 15:17
  • Hi Kendi, I use this, its not working good,i use different different animation for moving car and rotation but Above are showing good animation only problem is sometime marker rotate 360. I dn't know what is the problem. – Vaib Sep 09 '16 at 12:54

1 Answers1

0

Before calling these functions

rotateMarker(location,previcsLocation);
animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);

in your code. You'll have to add a check for the new (Latitude,Logitude) which comes through the server and marker's (Latitude,Logitude) if both values are same then you should avoid to call the above functions.

Only you have to add this condition in it.

 LatLng new_location = new LatLng(data.getLatitude(), data.getLongitude());
 //new_location is coming through the server
 LatLng current_location = marker.getPosition();
 //this is the marker's location 
 if (current_location != new_location) {



if (current_location.equals(new_location)) {
    Log.d(TAG, "LatLng " + "---------current_location-" + current_location);
    Log.d(TAG, "LatLng " + "---------new_location-" + new_location);
    return;
}


rotateMarker(location,previcsLocation);
animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);

}

It worked in my project.