Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void getDriverLocation() {
- retrofitInterface.getLiveTripDetails(routeDetails.getId()).enqueue(new Callback<List<TripDetails>>() {
- @Override
- public void onResponse(Call<List<TripDetails>> call, Response<List<TripDetails>> response) {
- if (response.isSuccessful()){
- if (response.body() != null && response.body().size() > 0) {
- for (int i = 0; i < response.body().size(); i++) {
- tripDetails = response.body().get(i);
- }
- double lat = tripDetails.getLastLat();
- double lng = tripDetails.getLastLong();
- if (lat != 0.0 && lng != 0.0) {
- if (user_marker != null && user_marker.isVisible()) {
- user_marker.remove();
- }
- bus_location = new LatLng(tripDetails.getLastLat(),
- tripDetails.getLastLong());
- if (!busLocationRequested) {
- oldLocation = bus_location;
- bus_marker = mMap.addMarker(new MarkerOptions()
- .position(bus_location)
- .title(routeDetails.getDriverName())
- .snippet(routeDetails.getDriverMobileNo())
- .rotation(toLocation(bus_location).getBearing())
- .anchor(0.5f, 0.5f)
- .flat(true)
- .icon(vectorToBitmap(R.drawable.ic_bus_top_view)));
- CameraPosition camPos = CameraPosition
- .builder(mMap.getCameraPosition())
- .bearing(toLocation(bus_location).getBearing())
- .target(bus_location)
- .zoom(18)
- .build();
- mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
- startTimer();
- }
- else {
- updateMarker();
- }
- addMarkers();
- }
- else {
- displayUserLocation();
- }
- }
- else {
- displayUserLocation();
- }
- }
- }
- @Override
- public void onFailure(Call<List<TripDetails>> call, Throwable t) {
- }
- });
- }
- private void updateMarker() {
- float rotation = (float) SphericalUtil.computeHeading(oldLocation, bus_location);
- rotateMarker(bus_markers.get(0), toLocation(bus_location), rotation);
- LatLngInterpolator latLngInterpolator = new LatLngInterpolator.Spherical();
- MarkerAnimation.animateMarkerToICS(bus_markers.get(0), bus_location, latLngInterpolator);
- oldLocation = bus_location;
- }
- public void rotateMarker(final Marker marker, final Location destination, final float rotation) {
- if (marker != null) {
- final LatLng startPosition = marker.getPosition();
- final LatLng endPosition = new LatLng(destination.getLatitude(), destination.getLongitude());
- final float startRotation = marker.getRotation();
- final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
- ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
- valueAnimator.setDuration(1500); // duration 1.5 seconds
- valueAnimator.setInterpolator(new LinearInterpolator());
- valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- @Override public void onAnimationUpdate(ValueAnimator animation) {
- try {
- float v = animation.getAnimatedFraction();
- LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
- float bearing = computeRotation(v, startRotation, rotation);
- /*marker.setPosition(newPosition);*/
- marker.setRotation(bearing);
- } catch (Exception ex) {
- // I don't care atm..
- }
- }
- });
- valueAnimator.start();
- }
- }
- private static float computeRotation(float fraction, float start, float end) {
- float normalizeEnd = end - start; // rotate start to 0
- float normalizedEndAbs = (normalizeEnd + 360) % 360;
- float direction = (normalizedEndAbs > 180) ? -1 : 1; // -1 = anticlockwise, 1 = clockwise
- float rotation;
- if (direction > 0) {
- rotation = normalizedEndAbs;
- } else {
- rotation = normalizedEndAbs - 360;
- }
- float result = fraction * rotation + start;
- return (result + 360) % 360;
- }
- private void displaySchoolLocation(){
- busLocationRequested = false;
- if (bus_markers != null && bus_markers.isVisible() {
- bus_markers.remove();
- }
- handler.removeCallbacks(runnable);
- UserDetails userDetails = getLocalStorageData().getUserDetails();
- LatLng user_location = userDetails.getLatLng();
- user_marker = mMap.addMarker(new MarkerOptions()
- .position(user_location)
- .title(userDetails.getName())
- .anchor(0.5f, 0.5f)
- .icon(vectorToBitmap(R.drawable.ic_user)));
- String message = "The bus has not yet started. Please come back by " + routeDetails.getDropTime();
- String bText = getString(R.string.check_again);
- final Snackbar snackbar = Snackbar.make(mLayout, message, LENGTH_INDEFINITE);
- snackbar.setAction(bText, new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- snackbar.dismiss();
- getDriverLocation();
- }
- });
- snackbar.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement