在谷歌地图中旋转标记/汽车图标 - Android

时间:2017-01-04 10:19:33

标签: android google-maps rotation

我正在尝试像ola / uber这样的应用程序。我想移动图标并在道路左转或右转时旋转。我正在使用以下代码。

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;
                    }
                }
            });
        }
    }

计算方位:

        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;
        }

设置方位:

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

要旋转标记,请在onLocationChanged更改方法中调用roateMarker方法:

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

现在我的图标正在旋转。但谷歌地图也变得旋转。我想单独旋转图标。我将以下链接用于动画和移动标记。 Link 1。请让我解决我的问题。

4 个答案:

答案 0 :(得分:4)

标记

有简单的方法
marker.rotation(float value)

围绕标记的锚点顺时针设置标记的旋转角度。旋转轴垂直于标记。旋转0对应于标记的默认位置。当标记在地图上是平的时,默认位置是北对齐,并且旋转使得标记始终在地图上保持平坦。当标记是广告牌时,默认位置朝上并且旋转使得标记始终面向摄像机。默认值为0.

答案 1 :(得分:1)

使用setRotation(float)方法仅将标记集旋转旋转到标记。

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);
                }
            }
        });
    }

答案 2 :(得分:1)

试试这个:

public void animateMarker(final LatLng toPosition, final LatLng startLatLng,
                          final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = map.getProjection();
    Point startPoint = proj.toScreenLocation(d_marker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
    //   final CameraPosition newcameraPosition = null;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {

            Location prevLoc = new Location("service Provider");
            prevLoc.setLatitude(startLatLng.latitude);
            prevLoc.setLongitude(startLatLng.longitude);

            Location newLoc = new Location("service Provider");
            newLoc.setLatitude(toPosition.latitude);
            newLoc.setLongitude(toPosition.longitude);

            System.out.println("Locations ---- " + prevLoc + "-" + newLoc);

            float bearing = prevLoc.bearingTo(newLoc);

            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed
                    / 1000);
            double lng = t * toPosition.longitude + (1 - t)
                    * startLatLng.longitude;
            double lat = t * toPosition.latitude + (1 - t)
                    * startLatLng.latitude;
            d_marker.setPosition(new LatLng(lat, lng));
            d_marker.setRotation(bearing);
            d_marker.setFlat(true);
            //   googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(newcameraPosition));


            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    d_marker.setVisible(false);
                } else {
                    d_marker.setVisible(true);
                }
            }
        }
    });


}

答案 3 :(得分:1)

尝试以下方法:

 public void animateMarker(final LatLng toPosition,
                          final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = googleMap.getProjection();
    Point startPoint = proj.toScreenLocation(cabMarker.getPosition());
    final LatLng startLatLng = proj.fromScreenLocation(startPoint);
 //   final CameraPosition newcameraPosition = null;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        @Override
        public void run() {

            Location prevLoc = new Location("service Provider");
            prevLoc.setLatitude(startLatLng.latitude);
            prevLoc.setLongitude(startLatLng.longitude);

            Location newLoc = new Location("service Provider");
            newLoc.setLatitude(toPosition.latitude);
            newLoc.setLongitude(toPosition.longitude);

            System.out.println("Locations ---- " + prevLoc + "-" + newLoc);

            float bearing = prevLoc.bearingTo(newLoc);

            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed
                    / 1000);
            double lng = t * toPosition.longitude + (1 - t)
                    * startLatLng.longitude;
            double lat = t * toPosition.latitude + (1 - t)
                    * startLatLng.latitude;
            cabMarker.setPosition(new LatLng(lat, lng));


            cabMarker.setRotation(bearing);
         //   googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(newcameraPosition));


            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    cabMarker.setVisible(false);
                } else {
                    cabMarker.setVisible(true);
                }
            }
        }
    });


}