Skip to content

Commit

Permalink
Initial work towards #176 - show real-time vehicle positions on map i…
Browse files Browse the repository at this point in the history
…n route mode

TODO:
* Vehicle map icon (including orientation of vehicle and popup balloon contents
* Cleanup of code related to icons
* Keep animation movement of buses?  Can be strange when bus moves off a road and cuts corners - should we snap it to the route line, if that is possible?
  • Loading branch information
barbeau committed Oct 12, 2015
1 parent a4d09b8 commit b6dda8b
Show file tree
Hide file tree
Showing 8 changed files with 1,144 additions and 60 deletions.
Expand Up @@ -47,6 +47,7 @@
import org.onebusaway.android.io.elements.ObaShape;
import org.onebusaway.android.io.elements.ObaStop;
import org.onebusaway.android.io.request.ObaResponse;
import org.onebusaway.android.io.request.ObaTripsForRouteResponse;
import org.onebusaway.android.map.MapModeController;
import org.onebusaway.android.map.MapParams;
import org.onebusaway.android.map.RouteMapController;
Expand Down Expand Up @@ -76,6 +77,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -128,6 +130,8 @@ public class BaseMapFragment extends SupportMapFragment
// is used by both modes.
private StopOverlay mStopOverlay;

private VehicleOverlay mVehicleOverlay;

// We only display the out of range dialog once
private boolean mWarnOutOfRange = true;

Expand Down Expand Up @@ -310,6 +314,12 @@ public void setupStopOverlay() {
}
}

public void setupVehicleOverlay() {
if (mVehicleOverlay == null) {
mVehicleOverlay = new VehicleOverlay(getActivity(), mMap);
}
}

protected void showDialog(int id) {
MapDialogFragment.newInstance(id, this).show(getFragmentManager(), MapDialogFragment.TAG);
}
Expand Down Expand Up @@ -668,6 +678,22 @@ public void setRouteOverlay(int lineOverlayColor, ObaShape[] shapes) {
}
}

/**
* Updates markers for the provided routeIds from the status info from the given ObaTripsForRouteResponse
* @param routeIds markers representing real-time positions for the provided routeIds will be added to the map
* @param response response that contains the real-time status info
*/
@Override
public void updateVehicles(HashSet<String> routeIds, ObaTripsForRouteResponse response) {
setupVehicleOverlay();
mVehicleOverlay.updateVehicles(routeIds, response);
}

@Override
public void removeVehicleOverlay() {
mVehicleOverlay.clear();
}

@Override
public void zoomToRoute() {
if (mMap != null) {
Expand Down
@@ -0,0 +1,34 @@
package org.onebusaway.android.util.test;

import org.onebusaway.android.io.test.ObaTestCase;
import org.onebusaway.android.util.MathUtils;

/**
* Tests to evaluate utility methods related to math conversions
*/
public class MathUtilTest extends ObaTestCase {

/**
* Tests conversion from OBA orientation to normal 0-360 degrees direction.
*
* From OBA REST API docs for trip status (http://developer.onebusaway.org/modules/onebusaway-application-modules/current/api/where/elements/trip-status.html)
* : "orientation - ...0º is east, 90º is north, 180º is west, and 270º is south."
*/
public void testOrientationToDirection() {
// East
double direction = MathUtils.toDirection(0);
assertEquals(90.0, direction);

// North
direction = MathUtils.toDirection(90);
assertEquals(0.0, direction);

// West
direction = MathUtils.toDirection(180);
assertEquals(270.0, direction);

// South
direction = MathUtils.toDirection(270);
assertEquals(180.0, direction);
}
}
@@ -0,0 +1,140 @@
/* Copyright 2013 Google Inc.
Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0.html
Source - https://gist.github.com/broady/6314689
Video - https://www.youtube.com/watch?v=WKfZsCKSXVQ&feature=youtu.be
*/

package org.onebusaway.android.map.googlemapsv2;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;

import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Property;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;

/**
* Animation utilities for markers with Maps API.
*
* Note - this class must remain in this .map.googleMapsv2 package so that the Google and Amazon
* build flavors work correctly.
*/
public class AnimationUtil {

/**
* Animates a marker from it's current position to the provided finalPosition
*
* @param marker marker to animate
* @param finalPosition the final position of the marker after the animation
*/
public static void animateMarkerTo(final Marker marker, final LatLng finalPosition) {
// Use the appropriate implementation per API Level
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
animateMarkerToICS(marker, finalPosition);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
animateMarkerToHC(marker, finalPosition);
} else {
animateMarkerToGB(marker, finalPosition);
}
}

private static void animateMarkerToGB(final Marker marker, final LatLng finalPosition) {
final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.Linear();
final LatLng startPosition = marker.getPosition();
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final Interpolator interpolator = new AccelerateDecelerateInterpolator();
final float durationInMs = 3000;

handler.post(new Runnable() {
long elapsed;

float t;

float v;

@Override
public void run() {
// Calculate progress using interpolator
elapsed = SystemClock.uptimeMillis() - start;
t = elapsed / durationInMs;
v = interpolator.getInterpolation(t);

marker.setPosition(latLngInterpolator.interpolate(v, startPosition, finalPosition));

// Repeat till progress is complete.
if (t < 1) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private static void animateMarkerToHC(final Marker marker, final LatLng finalPosition) {
final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.Linear();
final LatLng startPosition = marker.getPosition();

ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float v = animation.getAnimatedFraction();
LatLng newPosition = latLngInterpolator
.interpolate(v, startPosition, finalPosition);
marker.setPosition(newPosition);
}
});
valueAnimator.setFloatValues(0, 1); // Ignored.
valueAnimator.setDuration(3000);
valueAnimator.start();
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private static void animateMarkerToICS(Marker marker, LatLng finalPosition) {
final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.Linear();
TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {
@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
return latLngInterpolator.interpolate(fraction, startValue, endValue);
}
};
Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position");
ObjectAnimator animator = ObjectAnimator
.ofObject(marker, property, typeEvaluator, finalPosition);
animator.setDuration(3000);
animator.start();
}

/**
* For other LatLngInterpolator interpolators, see https://gist.github.com/broady/6314689
*/
interface LatLngInterpolator {

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

class Linear 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);
}
}
}
}
Expand Up @@ -36,6 +36,7 @@
import org.onebusaway.android.io.elements.ObaShape;
import org.onebusaway.android.io.elements.ObaStop;
import org.onebusaway.android.io.request.ObaResponse;
import org.onebusaway.android.io.request.ObaTripsForRouteResponse;
import org.onebusaway.android.map.MapModeController;
import org.onebusaway.android.map.MapParams;
import org.onebusaway.android.map.RouteMapController;
Expand Down Expand Up @@ -65,6 +66,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -117,6 +119,8 @@ public class BaseMapFragment extends SupportMapFragment
// is used by both modes.
private StopOverlay mStopOverlay;

private VehicleOverlay mVehicleOverlay;

// We only display the out of range dialog once
private boolean mWarnOutOfRange = true;

Expand Down Expand Up @@ -299,6 +303,12 @@ public void setupStopOverlay() {
}
}

public void setupVehicleOverlay() {
if (mVehicleOverlay == null) {
mVehicleOverlay = new VehicleOverlay(getActivity(), mMap);
}
}

protected void showDialog(int id) {
MapDialogFragment.newInstance(id, this).show(getFragmentManager(), MapDialogFragment.TAG);
}
Expand Down Expand Up @@ -657,6 +667,25 @@ public void setRouteOverlay(int lineOverlayColor, ObaShape[] shapes) {
}
}

/**
* Updates markers for the provided routeIds from the status info from the given
* ObaTripsForRouteResponse
*
* @param routeIds markers representing real-time positions for the provided routeIds will be
* added to the map
* @param response response that contains the real-time status info
*/
@Override
public void updateVehicles(HashSet<String> routeIds, ObaTripsForRouteResponse response) {
setupVehicleOverlay();
mVehicleOverlay.updateVehicles(routeIds, response);
}

@Override
public void removeVehicleOverlay() {
mVehicleOverlay.clear();
}

@Override
public void zoomToRoute() {
if (mMap != null) {
Expand Down

0 comments on commit b6dda8b

Please sign in to comment.