Skip to content

Instantly share code, notes, and snippets.

@MuhamedFathy
Created March 18, 2017 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MuhamedFathy/ac0c8f8ae79e36ac622f3a5e0c1821a4 to your computer and use it in GitHub Desktop.
Save MuhamedFathy/ac0c8f8ae79e36ac622f3a5e0c1821a4 to your computer and use it in GitHub Desktop.
public class Animator implements Runnable {
private static final int ANIMATE_SPEEED = 800;
private static final int ANIMATE_SPEEED_TURN = 1000;
private static final int BEARING_OFFSET = 20;
private final Interpolator interpolator = new LinearInterpolator();
int currentIndex = 0;
float tilt = 90;
float zoom = 15.5f;
boolean upward = true;
long start = SystemClock.uptimeMillis();
LatLng endLatLng = null;
LatLng beginLatLng = null;
boolean showPolyline = false;
private Marker trackingMarker;
public void reset() {
resetMarkers();
start = SystemClock.uptimeMillis();
currentIndex = 0;
endLatLng = getEndLatLng();
beginLatLng = getBeginLatLng();
}
public void stop() {
trackingMarker.remove();
mHandler.removeCallbacks(animator);
}
public void initialize(boolean showPolyLine) {
reset();
this.showPolyline = showPolyLine;
highLightMarker(0);
if (showPolyLine) {
polyLine = initializePolyLine();
}
// We first need to put the camera in the correct position for the first run (we need 2 markers for this).....
LatLng markerPos = markers.get(0).getPosition();
LatLng secondPos = markers.get(1).getPosition();
setupCameraPositionForMovement(markerPos, secondPos);
}
private void setupCameraPositionForMovement(LatLng markerPos,
LatLng secondPos) {
float bearing = bearingBetweenLatLngs(markerPos, secondPos);
trackingMarker = googleMap.addMarker(new MarkerOptions().position(markerPos)
.title("title")
.snippet("snippet"));
CameraPosition cameraPosition =
new CameraPosition.Builder()
.target(markerPos)
.bearing(bearing + BEARING_OFFSET)
.tilt(90)
.zoom(googleMap.getCameraPosition().zoom >= 16 ? googleMap.getCameraPosition().zoom
: 16)
.build();
googleMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPosition),
ANIMATE_SPEEED_TURN,
new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
System.out.println("finished camera");
animator.reset();
Handler handler = new Handler();
handler.post(animator);
}
@Override
public void onCancel() {
System.out.println("cancelling camera");
}
}
);
}
private Polyline polyLine;
private PolylineOptions rectOptions = new PolylineOptions();
private Polyline initializePolyLine() {
//polyLinePoints = new ArrayList<LatLng>();
rectOptions.add(markers.get(0).getPosition());
return googleMap.addPolyline(rectOptions);
}
/**
* Add the marker to the polyline.
*/
private void updatePolyLine(LatLng latLng) {
List<LatLng> points = polyLine.getPoints();
points.add(latLng);
polyLine.setPoints(points);
}
public void stopAnimation() {
animator.stop();
}
public void startAnimation(boolean showPolyLine) {
if (markers.size() > 2) {
animator.initialize(showPolyLine);
}
}
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
double t = interpolator.getInterpolation((float) elapsed / ANIMATE_SPEEED);
// LatLng endLatLng = getEndLatLng();
// LatLng beginLatLng = getBeginLatLng();
double lat = t * endLatLng.latitude + (1 - t) * beginLatLng.latitude;
double lng = t * endLatLng.longitude + (1 - t) * beginLatLng.longitude;
LatLng newPosition = new LatLng(lat, lng);
trackingMarker.setPosition(newPosition);
if (showPolyline) {
updatePolyLine(newPosition);
}
// It's not possible to move the marker + center it through a cameraposition update while another camerapostioning was already happening.
//navigateToPoint(newPosition,tilt,bearing,currentZoom,false);
//navigateToPoint(newPosition,false);
if (t < 1) {
mHandler.postDelayed(this, 16);
} else {
System.out.println(
"Move to next marker.... current = " + currentIndex + " and size = " + markers.size());
// imagine 5 elements - 0|1|2|3|4 currentindex must be smaller than 4
if (currentIndex < markers.size() - 2) {
currentIndex++;
endLatLng = getEndLatLng();
beginLatLng = getBeginLatLng();
start = SystemClock.uptimeMillis();
LatLng begin = getBeginLatLng();
LatLng end = getEndLatLng();
float bearingL = bearingBetweenLatLngs(begin, end);
highLightMarker(currentIndex);
CameraPosition cameraPosition =
new CameraPosition.Builder()
.target(end) // changed this...
.bearing(bearingL + BEARING_OFFSET)
.tilt(tilt)
.zoom(googleMap.getCameraPosition().zoom)
.build();
googleMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPosition),
ANIMATE_SPEEED_TURN,
null
);
start = SystemClock.uptimeMillis();
mHandler.postDelayed(animator, 16);
} else {
currentIndex++;
highLightMarker(currentIndex);
stopAnimation();
}
}
}
private LatLng getEndLatLng() {
return markers.get(currentIndex + 1).getPosition();
}
private LatLng getBeginLatLng() {
return markers.get(currentIndex).getPosition();
}
private void adjustCameraPosition() {
//System.out.println("tilt = " + tilt);
//System.out.println("upward = " + upward);
//System.out.println("zoom = " + zoom);
if (upward) {
if (tilt < 90) {
tilt++;
zoom -= 0.01f;
} else {
upward = false;
}
} else {
if (tilt > 0) {
tilt--;
zoom += 0.01f;
} else {
upward = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment