android 地图标记旋转,android – 在谷歌地图v2中动画标记的旋转

这是我的实现平滑标记移动与制造商图像旋转(当它设置为FLAT [重要]).标记被平滑地移动到所请求的位置,并且它以适当的方向旋转到所需的程度.例如,当从5deg移动到355deg时,它会逆时针移动,当355deg移动到5deg时,它会顺时针移动.

public void animateMarker(final Location location)

{

if (myMarkerLOC != null) {

final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();

ValueAnimator valueAnimator = new ValueAnimator();

final LatLng startPosition = myMarkerLOC.getPosition();

final float startRotation = myMarkerLOC.getRotation();

final float angle = 180 - Math.abs(Math.abs(startRotation - location.getBearing()) - 180);

final float right = WhichWayToTurn(startRotation, location.getBearing());

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()

{

@Override

public void onAnimationUpdate(ValueAnimator animation)

{

try {

if (myMarkerLOC == null) // oops... destroying map during animation...

{

return;

}

float v = animation.getAnimatedFraction();

LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, PositionUtil.toLatLng(location));

float rotation = startRotation + right * v * angle;

myMarkerLOC.setRotation((float) rotation);

myMarkerLOC.setPosition(newPosition);

} catch (Exception ex) {

// I don't care atm..

}

}

});

valueAnimator.setFloatValues(0, 1);

valueAnimator.setDuration(300);

valueAnimator.start();

}

}

private float WhichWayToTurn(float currentDirection, float targetDirection)

{

float diff = targetDirection - currentDirection;

if (Math.abs(diff) == 0)

{

return 0;

}

if(diff > 180)

{

return -1;

}

else

{

return 1;

}

}

public interface LatLngInterpolator

{

public LatLng interpolate(float fraction, LatLng a, LatLng b);

public class Linear implements LatLngInterpolator

{

@Override

public LatLng interpolate(float fraction, LatLng a, LatLng b)

{

double lat = (b.latitude - a.latitude) * fraction + a.latitude;

double lng = (b.longitude - a.longitude) * fraction + a.longitude;

return new LatLng(lat, lng);

}

}

public class LinearFixed implements LatLngInterpolator

{

@Override

public LatLng interpolate(float fraction, LatLng a, LatLng b) {

double lat = (b.latitude - a.latitude) * fraction + a.latitude;

double lngDelta = b.longitude - a.longitude;

// Take the shortest path across the 180th meridian.

if (Math.abs(lngDelta) > 180) {

lngDelta -= Math.signum(lngDelta) * 360;

}

double lng = lngDelta * fraction + a.longitude;

return new LatLng(lat, lng);

}

}

}

缺少“toLatLong”方法的实现:

public static LatLng toLatLng(final Location location)

{

return new LatLng(location.getLatitude(), location.getLongitude());

}

我希望有所帮助.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值