简体   繁体   English

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

[英]Rotate marker/car icon in google maps - Android

I am trying to crate a app like ola/uber.我正在尝试创建一个像 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:要旋转标记,我在onLocationChanged更改方法中调用 roateMarker 方法:

        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 .链接 1 Please let me any idea to solve my issue.请让我有任何想法来解决我的问题。

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.旋转 0 对应于标记的默认位置。 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.默认值为 0。

To rotate only the marker set rotation to marker using setRotation(float) method.使用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);
                }
            }
        });
    }

Try this :尝试这个 :

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


}

Try Below method:试试下面的方法:

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM