Animated markers, need to customize behavior on newTimeStamp

This commit is contained in:
Adam Domagalski 2018-10-31 01:13:05 +01:00
parent ca26b03d49
commit c67d4ba4c8

View File

@ -25,6 +25,7 @@ import com.uam.wmi.findmytutor.network.RetrofitClientInstance;
import com.uam.wmi.findmytutor.service.CoordinateService;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -37,10 +38,10 @@ import io.reactivex.schedulers.Schedulers;
public class MapActivity extends BaseActivity
implements OnMapReadyCallback {
implements OnMapReadyCallback {
String tag = getClass().getName();
String tag = "LifeCycleEvents";
private MapView mapView;
@ -48,7 +49,9 @@ public class MapActivity extends BaseActivity
private int mInterval = 10000;
private Handler mHandler;
private Map<String,Coordinate> coordsMap = new HashMap<>();
private HashMap<String, Coordinate> coordsMap = new HashMap<>();
private HashMap<String, Marker> markerHash = new HashMap<>();
private CoordinateService coordinateService;
private CompositeDisposable disposable = new CompositeDisposable();
@ -56,21 +59,18 @@ public class MapActivity extends BaseActivity
private Runnable mStatusChecker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
configureLogoutButton();
final SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
final String authToken = sharedPref.getString("authToken",null);
final String authToken = sharedPref.getString("authToken", null);
mStatusChecker = new Runnable() {
@Override
public void run() {
try{
try {
fetchTopCoords();
} finally {
mHandler.postDelayed(mStatusChecker, mInterval);
@ -78,7 +78,7 @@ public class MapActivity extends BaseActivity
}
};
coordinateService = RetrofitClientInstance.createService(CoordinateService.class,"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJhM2MxMDU1YS1kZDM0LTQ5ZWItYTFkNS0xY2E5YTE2YzY0ODgiLCJzdWIiOiJzdHJpbmciLCJqdGkiOiI4NTA0NDA5NS00NjBkLTQzZDgtYjMxMC0xYmNiNWMxNGExZjQiLCJleHAiOjE1NDMzNTAzMzQsImlzcyI6Imh0dHA6Ly9maW5kbXl0dXRvci5jb20iLCJhdWQiOiJodHRwOi8vZmluZG15dHV0b3IuY29tIn0.xGyu6iBeq9xF0ufBd01jNzILLq1NeYa-5MeVOiPahD8");
coordinateService = RetrofitClientInstance.createService(CoordinateService.class, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJhM2MxMDU1YS1kZDM0LTQ5ZWItYTFkNS0xY2E5YTE2YzY0ODgiLCJzdWIiOiJzdHJpbmciLCJqdGkiOiI4NTA0NDA5NS00NjBkLTQzZDgtYjMxMC0xYmNiNWMxNGExZjQiLCJleHAiOjE1NDMzNTAzMzQsImlzcyI6Imh0dHA6Ly9maW5kbXl0dXRvci5jb20iLCJhdWQiOiJodHRwOi8vZmluZG15dHV0b3IuY29tIn0.xGyu6iBeq9xF0ufBd01jNzILLq1NeYa-5MeVOiPahD8");
mHandler = new Handler();
Bundle extras = getIntent().getExtras();
@ -94,7 +94,7 @@ public class MapActivity extends BaseActivity
public void onMapReady(MapboxMap map) {
mapboxMap = map;
final Marker marker = mapboxMap.addMarker(new MarkerViewOptions()
.position(new LatLng(52.466782,16.927549)));
.position(new LatLng(52.466782, 16.927549)));
mStatusChecker.run();
mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
@ -111,7 +111,7 @@ public class MapActivity extends BaseActivity
});
}
private static class LatLngEvaluator implements TypeEvaluator<LatLng> {
private static class LatLngEvaluator implements TypeEvaluator<LatLng> {
// Method is used to interpolate the marker animation.
private LatLng latLng = new LatLng();
@ -126,7 +126,7 @@ public class MapActivity extends BaseActivity
}
}
private void fetchTopCoords() {
private void fetchTopCoords() {
disposable.add(
// coordinateService.getTopCoordinates()
coordinateService.getOnlineCoordinates()
@ -136,56 +136,62 @@ public class MapActivity extends BaseActivity
@Override
public void onSuccess(List<Coordinate> coordsList) {
Log.e(tag, "");
for (Coordinate element : coordsList) {
String id = element.getUserId();
Coordinate cord = coordsMap.get(id);
Coordinate coordinate = coordsMap.get(id);
Log.e(tag, "hashMapSize: "+ coordsMap.size());
Log.e(tag, "markerMapSize: "+ coordsMap.size());
Log.d("mapper", "a " + mapboxMap.getMarkerViewManager());
Log.d("mapper", "b " + coordsMap.size());
if (cord != null) {
if (!cord.getLongitude().equals(element.getLongitude())
) {
Log.d("mapper", " cos sie zienilo ");
Marker marker = mapboxMap.addMarker(new MarkerViewOptions()
.title(cord.getUserId())
.position(new LatLng(cord.getLatitude(),cord.getLongitude())));
if (coordinate != null) {
Log.e(tag, "Coordin: " + coordinate.getLatitude()+" | " + coordinate.getLongitude());
// Log.e(tag, "Element: " + element.getLatitude()+" | " + element.getLongitude());
// Log.e(tag, "isEqual: " + coordinate.getLatitude().equals(element.getLatitude()));
// Log.e(tag, "isEqual: " + coordinate.getLongitude().equals(element.getLongitude()));
boolean statement = coordinate.getLatitude().equals(element.getLatitude()) || coordinate.getLongitude().equals(element.getLongitude());
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
new LatLngEvaluator(), marker.getPosition(), new LatLng(element.getLatitude(),element.getLongitude()));
Log.e(tag, "diff || diff: " + !statement);
if (!statement) {
Log.e(tag, "replace and animate");
ValueAnimator markerAnimator = ObjectAnimator.ofObject(markerHash.get(id), "position",
new LatLngEvaluator(), markerHash.get(id).getPosition(),
new LatLng(element.getLatitude(), element.getLongitude()));
markerAnimator.setDuration(2000);
markerAnimator.start();
coordsMap.put(id, element);
markerHash.get(id).setPosition(new LatLng(element.getLatitude(), element.getLongitude()));
} else if(!coordinate.getTimeStamp().equals(element.getTimeStamp())) {
Log.e(tag, "timestamp changed");
// coordsMap.replace(id,element);
coordsMap.remove(id);
coordsMap.put(id,element);
} else if (!cord.getTimeStamp().equals(element.getTimeStamp())){
Log.d("mapper", "update");
Log.d("mapper", " "+cord.getTimeStamp());
Log.d("mapper", " " + element.getTimeStamp());
// coordsMap.replace(id,element);
coordsMap.remove(id);
coordsMap.put(id,element);
}
} else {
coordsMap.put(id,element);
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(element.getLatitude(), element.getLongitude())));
Log.e(tag, "Marker Added: " + id);
coordsMap.put(id, element);
Marker marker = mapboxMap.addMarker(new MarkerOptions()
.title(element.getLabel())
.position(new LatLng(element.getLatitude(), element.getLongitude())));
markerHash.put(id, marker);
}
}
}
@Override
public void onError(Throwable e) {
Log.e("Error",e.toString());
Log.e("Error", e.toString());
}
})
);
}
private void configureLogoutButton(){
private void configureLogoutButton() {
// Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton);
@ -203,7 +209,6 @@ public class MapActivity extends BaseActivity
}
// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
@ -239,7 +244,7 @@ public class MapActivity extends BaseActivity
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
mHandler.removeCallbacks(mStatusChecker);
mHandler.removeCallbacks(mStatusChecker);
disposable.dispose();
}