Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. private void getDriverLocation() {
  2.  
  3. retrofitInterface.getLiveTripDetails(routeDetails.getId()).enqueue(new Callback<List<TripDetails>>() {
  4. @Override
  5. public void onResponse(Call<List<TripDetails>> call, Response<List<TripDetails>> response) {
  6. if (response.isSuccessful()){
  7. if (response.body() != null && response.body().size() > 0) {
  8. for (int i = 0; i < response.body().size(); i++) {
  9. tripDetails = response.body().get(i);
  10. }
  11.  
  12. double lat = tripDetails.getLastLat();
  13. double lng = tripDetails.getLastLong();
  14.  
  15. if (lat != 0.0 && lng != 0.0) {
  16.  
  17. if (user_marker != null && user_marker.isVisible()) {
  18. user_marker.remove();
  19. }
  20.  
  21. bus_location = new LatLng(tripDetails.getLastLat(),
  22. tripDetails.getLastLong());
  23.  
  24. if (!busLocationRequested) {
  25.  
  26. oldLocation = bus_location;
  27.  
  28. bus_marker = mMap.addMarker(new MarkerOptions()
  29. .position(bus_location)
  30. .title(routeDetails.getDriverName())
  31. .snippet(routeDetails.getDriverMobileNo())
  32. .rotation(toLocation(bus_location).getBearing())
  33. .anchor(0.5f, 0.5f)
  34. .flat(true)
  35. .icon(vectorToBitmap(R.drawable.ic_bus_top_view)));
  36.  
  37. CameraPosition camPos = CameraPosition
  38. .builder(mMap.getCameraPosition())
  39. .bearing(toLocation(bus_location).getBearing())
  40. .target(bus_location)
  41. .zoom(18)
  42. .build();
  43. mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
  44.  
  45. startTimer();
  46. }
  47. else {
  48. updateMarker();
  49. }
  50.  
  51. addMarkers();
  52.  
  53. }
  54. else {
  55. displayUserLocation();
  56. }
  57. }
  58. else {
  59. displayUserLocation();
  60. }
  61. }
  62. }
  63.  
  64. @Override
  65. public void onFailure(Call<List<TripDetails>> call, Throwable t) {
  66.  
  67. }
  68. });
  69. }
  70.  
  71. private void updateMarker() {
  72.  
  73. float rotation = (float) SphericalUtil.computeHeading(oldLocation, bus_location);
  74. rotateMarker(bus_markers.get(0), toLocation(bus_location), rotation);
  75.  
  76. LatLngInterpolator latLngInterpolator = new LatLngInterpolator.Spherical();
  77. MarkerAnimation.animateMarkerToICS(bus_markers.get(0), bus_location, latLngInterpolator);
  78.  
  79. oldLocation = bus_location;
  80. }
  81.  
  82. public void rotateMarker(final Marker marker, final Location destination, final float rotation) {
  83. if (marker != null) {
  84. final LatLng startPosition = marker.getPosition();
  85. final LatLng endPosition = new LatLng(destination.getLatitude(), destination.getLongitude());
  86.  
  87. final float startRotation = marker.getRotation();
  88.  
  89. final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
  90. ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
  91. valueAnimator.setDuration(1500); // duration 1.5 seconds
  92. valueAnimator.setInterpolator(new LinearInterpolator());
  93. valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  94. @Override public void onAnimationUpdate(ValueAnimator animation) {
  95. try {
  96. float v = animation.getAnimatedFraction();
  97.  
  98. LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
  99. float bearing = computeRotation(v, startRotation, rotation);
  100.  
  101. /*marker.setPosition(newPosition);*/
  102. marker.setRotation(bearing);
  103.  
  104. } catch (Exception ex) {
  105. // I don't care atm..
  106. }
  107. }
  108. });
  109. valueAnimator.start();
  110. }
  111. }
  112. private static float computeRotation(float fraction, float start, float end) {
  113. float normalizeEnd = end - start; // rotate start to 0
  114. float normalizedEndAbs = (normalizeEnd + 360) % 360;
  115.  
  116. float direction = (normalizedEndAbs > 180) ? -1 : 1; // -1 = anticlockwise, 1 = clockwise
  117. float rotation;
  118. if (direction > 0) {
  119. rotation = normalizedEndAbs;
  120. } else {
  121. rotation = normalizedEndAbs - 360;
  122. }
  123.  
  124. float result = fraction * rotation + start;
  125. return (result + 360) % 360;
  126. }
  127.  
  128. private void displaySchoolLocation(){
  129. busLocationRequested = false;
  130.  
  131. if (bus_markers != null && bus_markers.isVisible() {
  132. bus_markers.remove();
  133. }
  134.  
  135. handler.removeCallbacks(runnable);
  136.  
  137. UserDetails userDetails = getLocalStorageData().getUserDetails();
  138. LatLng user_location = userDetails.getLatLng();
  139. user_marker = mMap.addMarker(new MarkerOptions()
  140. .position(user_location)
  141. .title(userDetails.getName())
  142. .anchor(0.5f, 0.5f)
  143. .icon(vectorToBitmap(R.drawable.ic_user)));
  144.  
  145. String message = "The bus has not yet started. Please come back by " + routeDetails.getDropTime();
  146. String bText = getString(R.string.check_again);
  147. final Snackbar snackbar = Snackbar.make(mLayout, message, LENGTH_INDEFINITE);
  148. snackbar.setAction(bText, new View.OnClickListener() {
  149. @Override
  150. public void onClick(View v) {
  151. snackbar.dismiss();
  152. getDriverLocation();
  153. }
  154. });
  155. snackbar.show();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement