Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Problem in Google map rotation in Android

    • 0
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 1.60k
    Answer it

    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) {
                   
                        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) {
                            // Post again 16ms later.
                            handler.postDelayed(this, 16);
                        } else {
                            isMarkerRotating = false;
                        }
                    }
                });
            }
        }
    
    // method call
    rotateMarker(location,previcsLocation);
                animateMarkr(location.getLatitude(), location.getLongitude(),previcsLocation);

     

 4 Answer(s)

  • Hi devesh, thanks for the help. But the code that you have mentioned is for moving the car and I think the problem is with the rotation animation code. If we are not including the same (the rotation code) the car is moving smoothly but the turns that car is taking are not smooth for the same we have introduced the rotation code. After applying the same the car is revolving 360 degree sometime like 2 or 3 times in a trip. I have to stop the same "the car revolving" I am not getting the reason why it is behaving so and it is happening occasionally not every time. Like after travelling some distance randomly it revolve at any point 1 or 2 time and then it moves smoothly. The car animation of 360 degree that is happening occasionally I have to stop that.

  • I have implemented the same piece of code and it worked liked a charm.

    private void animateMarker(LatLng latlng,final Marker marker){
            final LatLng target = latlng;
    
            final long duration = 800;
            final Handler handler = new Handler();
            final long start = SystemClock.uptimeMillis();
            Projection proj = mMap.getProjection();
    
            Point startPoint = proj.toScreenLocation(marker.getPosition());
            final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    
            final Interpolator interpolator = new LinearInterpolator();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    long elapsed = SystemClock.uptimeMillis() - start;
                    float t = interpolator.getInterpolation((float) elapsed / duration);
                    double lng = t * target.longitude + (1 - t) * startLatLng.longitude;
                    double lat = t * target.latitude + (1 - t) * startLatLng.latitude;
                    marker.setPosition(new LatLng(lat, lng));
                    if (t < 1.0) {
                        // Post again 10ms later.
                        handler.postDelayed(this, 10);
                    } else {
                        // animation ended
                    }
                }
            });
        }
    
  • Hi Devesh, yes I want to implement the translate animation. The only thing that I am missing out is sometime the car is moving at 360 degree clockwise and anticlockwise as well on map and I am not getting the reason of behavior of the same Apart from that car is moving smoothly .

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: