标记隐藏标记的Android setRotation

问题描述 投票:0回答:1

我想按照方向移动和旋转标记,移动标记工作正常但是当我添加标记旋转它隐藏标记时,我使用以下代码进行标记移动动画和旋转当我评论setRotation()方法它完美地工作但是当我取消注释setRotation()将它隐藏在位置更新标记上

public void animateMarker(final LatLng toPosition,final LatLng neLatLong,
                          final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    final long duration = 200;

    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);
            curMarker.setPosition(toPosition);
            curMarker.setRotation(getBearing(toPosition,neLatLong));

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


private float getBearing(LatLng begin, LatLng end) {
    double lat = Math.abs(begin.latitude - end.latitude);
    double lng = Math.abs(begin.longitude - end.longitude);

    if (begin.latitude < end.latitude && begin.longitude < end.longitude)
        return (float) (Math.toDegrees(Math.atan(lng / lat)));
    else if (begin.latitude >= end.latitude && begin.longitude < end.longitude)
        return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 90);
    else if (begin.latitude >= end.latitude && begin.longitude >= end.longitude)
        return (float) (Math.toDegrees(Math.atan(lng / lat)) + 180);
    else if (begin.latitude < end.latitude && begin.longitude >= end.longitude)
        return (float) ((90 - Math.toDegrees(Math.atan(lng / lat))) + 270);
    return -1;
}
android google-maps google-maps-markers
1个回答
0
投票

确保为你的标记设置.anchor(0.5f, 0.5f)并更好地使用SphericalUtil.computeHeading()Google Maps Android API Utility Library 而不是你的private float getBearing(LatLng begin, LatLng end)

© www.soinside.com 2019 - 2024. All rights reserved.