Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate marker/car icon in google maps - Android

I am trying to crate a app like ola/uber. I want to move the icon and rotate when road turn left or right. I am using following code.

private void rotateMarker(final Marker marker, final float toRotation) {
        if(!isMarkerRotating) {
            final Handler handler = new Handler();
            final long start = SystemClock.uptimeMillis();
            final float startRotation = marker.getRotation();
            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;

                    marker.setRotation(-rot > 180 ? rot / 2 : rot);
                    if (t < 1.0) {
                        // Post again 16ms later.
                        handler.postDelayed(this, 16);
                    } else {
                        isMarkerRotating = false;
                    }
                }
            });
        }
    }

To calculate bearing:

        currentLocation = location;
        if(previousLocaton!=null){
            previousLocaton = tempLocation;
            tempLocation = currentLocation;

            Log.d("previousLocaton=====> ",""+previousLocaton);
            Log.d("currentLocation=====> ",""+currentLocation);

            bearing = previousLocaton.bearingTo(currentLocation) ;
        }else{
            previousLocaton = location;
            tempLocation = location;
        }

To set the bearing:

CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(latLng).zoom(14).bearing(bearing).build();

To rotate the marker I call roateMarker method in onLocationChanged changed method:

        currLocationMarker = mMap.addMarker(markerOptions);
        rotateMarker(currLocationMarker,bearing);

Now my icon is rotating. But google map also get rotating. I want rotate icon alone. I refer the following link for animate and move the marker. Link 1. Please let me any idea to solve my issue.

like image 206
Vijay Avatar asked Jan 04 '17 10:01

Vijay


2 Answers

there is simple method available for marker

marker.rotation(float value)

Sets the rotation of the marker in degrees clockwise about the marker's anchor point. The axis of rotation is perpendicular to the marker. A rotation of 0 corresponds to the default position of the marker. When the marker is flat on the map, the default position is North aligned and the rotation is such that the marker always remains flat on the map. When the marker is a billboard, the default position is pointing up and the rotation is such that the marker is always facing the camera. The default value is 0.

like image 195
raj Avatar answered Sep 28 '22 11:09

raj


To rotate only the marker set rotation to marker using setRotation(float) method.

static public void rotateMarker(final Marker marker, final float toRotation) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final float startRotation = marker.getRotation();
        final long duration = 1000;

        final Interpolator interpolator = new LinearInterpolator();
        L.d("Bearing: "+toRotation);

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

                float rot = t * toRotation + (1 - t) * startRotation;
                marker.setRotation(-rot > 180 ? rot / 2 : rot);
                if (t < 1.0) {
                    // Post again 10ms later.
                    handler.postDelayed(this, 10);
                }
            }
        });
    }
like image 44
Suresh Kumar Avatar answered Sep 28 '22 12:09

Suresh Kumar