Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. /**
  2. * Method to animate marker to destination location
  3. * @param destination destination location (must contain bearing attribute, to ensure
  4. * marker rotation will work correctly)
  5. * @param marker marker to be animated
  6. */
  7. public static void animateMarker(Location destination, Marker marker) {
  8. if (marker != null) {
  9. LatLng startPosition = marker.getPosition();
  10. LatLng endPosition = new LatLng(destination.getLatitude(), destination.getLongitude());
  11.  
  12. float startRotation = marker.getRotation();
  13.  
  14. LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
  15. ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
  16. valueAnimator.setDuration(1000); // duration 1 second
  17. valueAnimator.setInterpolator(new LinearInterpolator());
  18. valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  19. @Override public void onAnimationUpdate(ValueAnimator animation) {
  20. try {
  21. float v = animation.getAnimatedFraction();
  22. LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
  23. marker.setPosition(newPosition);
  24. marker.setRotation(computeRotation(v, startRotation, destination.getBearing()));
  25. } catch (Exception ex) {
  26. // I don't care atm..
  27. }
  28. }
  29. });
  30.  
  31. valueAnimator.start();
  32. }
  33. }
  34.  
  35. /**
  36. * Method to compute rotation (or marker's bearing) for specified fraction of animation.
  37. * Marker is rotated in the direction which is closer from start to end.
  38. *
  39. * @param fraction Fraction of animation completed b/w start and end location
  40. * @param start Rotation (or Bearing) for animation's start location
  41. * @param end Rotation (or Bearing) for animation's end location
  42. **/
  43. private static float computeRotation(float fraction, float start, float end) {
  44. float normalizeEnd = end - start; // rotate start to 0
  45. float normalizedEndAbs = (normalizeEnd + 360) % 360;
  46.  
  47. float direction = (normalizedEndAbs > 180) ? -1 : 1; // -1 = anticlockwise, 1 = clockwise
  48. float rotation;
  49. if (direction > 0) {
  50. rotation = normalizedEndAbs;
  51. } else {
  52. rotation = normalizedEndAbs - 360;
  53. }
  54.  
  55. float result = fraction * rotation + start;
  56. return (result + 360) % 360;
  57. }
  58.  
  59. private interface LatLngInterpolator {
  60. LatLng interpolate(float fraction, LatLng a, LatLng b);
  61.  
  62. class LinearFixed implements LatLngInterpolator {
  63. @Override
  64. public LatLng interpolate(float fraction, LatLng a, LatLng b) {
  65. double lat = (b.latitude - a.latitude) * fraction + a.latitude;
  66. double lngDelta = b.longitude - a.longitude;
  67. // Take the shortest path across the 180th meridian.
  68. if (Math.abs(lngDelta) > 180) {
  69. lngDelta -= Math.signum(lngDelta) * 360;
  70. }
  71. double lng = lngDelta * fraction + a.longitude;
  72. return new LatLng(lat, lng);
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement