【问题标题】:how to smoothly slow down the rotation speed of marker in google map如何平滑地减慢谷歌地图中标记的旋转速度
【发布时间】:2019-03-06 11:32:19
【问题描述】:

我用这条线来旋转标记

marker.setRotation((float) bearingBetweenLocations(startPosition, destination));

这会导致突然快速旋转。他们有什么方法可以减慢标记旋转速度

【问题讨论】:

    标签: android google-maps-markers android-maps-v2


    【解决方案1】:

    使用喜欢

    float bearing = (float) bearingBetweenLocations(startPosition, new LatLng(destination.getLatitude(), destination.getLongitude()));
                            // mMarker.setRotation((float) bearingBetweenLocations();
                            rotateMarker(mMarker, bearing);
    

    方法是

    您可以在此处更改旋转标记功能final long duration = 500; // Change duration as you want

     public double bearingBetweenLocations(LatLng latLng1, LatLng latLng2) {
            double PI = 3.14159;
            double lat1 = latLng1.latitude * PI / 180;
            double long1 = latLng1.longitude * PI / 180;
            double lat2 = latLng2.latitude * PI / 180;
            double long2 = latLng2.longitude * PI / 180;
    
            double dLon = (long2 - long1);
    
            double y = Math.sin(dLon) * Math.cos(lat2);
            double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
                    * Math.cos(lat2) * Math.cos(dLon);
    
            double brng = Math.atan2(y, x);
    
            brng = Math.toDegrees(brng);
            brng = (brng + 360) % 360;
    
            return brng;
        }
    
    
        public 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 = 500; // Change duration as you want
    
                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;
    
                        float bearing = -rot > 180 ? rot / 2 : rot;
    
                        marker.setRotation(bearing);
    
                        if (t < 1.0) {
                            // Post again 16ms later.
                            handler.postDelayed(this, 4);
                        } else {
                            isMarkerRotating = false;
                        }
                    }
                });
            }
        }
    

    希望对你有帮助。

    【讨论】:

    • 快乐编码 :-)
    • 有时它会旋转 360 度小转 y 就是这样
    • 您需要根据您的要求计算数值
    猜你喜欢
    • 1970-01-01
    • 2020-07-24
    • 2012-05-20
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多