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.service.CoordinateService;
import com.uam.wmi.findmytutor.utils.PrefUtils; import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.nio.ByteBuffer;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -39,8 +40,8 @@ import io.reactivex.schedulers.Schedulers;
public class MapActivity extends BaseActivity public class MapActivity extends BaseActivity
implements OnMapReadyCallback { implements OnMapReadyCallback {
String tag = getClass().getName();
String tag = "LifeCycleEvents";
private MapView mapView; private MapView mapView;
@ -48,7 +49,9 @@ public class MapActivity extends BaseActivity
private int mInterval = 10000; private int mInterval = 10000;
private Handler mHandler; 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 CoordinateService coordinateService;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
@ -56,21 +59,18 @@ public class MapActivity extends BaseActivity
private Runnable mStatusChecker; private Runnable mStatusChecker;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
configureLogoutButton(); configureLogoutButton();
final SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE); 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() { mStatusChecker = new Runnable() {
@Override @Override
public void run() { public void run() {
try{ try {
fetchTopCoords(); fetchTopCoords();
} finally { } finally {
mHandler.postDelayed(mStatusChecker, mInterval); 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(); mHandler = new Handler();
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
@ -94,7 +94,7 @@ public class MapActivity extends BaseActivity
public void onMapReady(MapboxMap map) { public void onMapReady(MapboxMap map) {
mapboxMap = map; mapboxMap = map;
final Marker marker = mapboxMap.addMarker(new MarkerViewOptions() final Marker marker = mapboxMap.addMarker(new MarkerViewOptions()
.position(new LatLng(52.466782,16.927549))); .position(new LatLng(52.466782, 16.927549)));
mStatusChecker.run(); mStatusChecker.run();
mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() { mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
@ -136,56 +136,62 @@ public class MapActivity extends BaseActivity
@Override @Override
public void onSuccess(List<Coordinate> coordsList) { public void onSuccess(List<Coordinate> coordsList) {
Log.e(tag, "");
for (Coordinate element : coordsList) { for (Coordinate element : coordsList) {
String id = element.getUserId(); 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()); if (coordinate != null) {
Log.d("mapper", "b " + coordsMap.size()); Log.e(tag, "Coordin: " + coordinate.getLatitude()+" | " + coordinate.getLongitude());
if (cord != null) { // Log.e(tag, "Element: " + element.getLatitude()+" | " + element.getLongitude());
if (!cord.getLongitude().equals(element.getLongitude()) // Log.e(tag, "isEqual: " + coordinate.getLatitude().equals(element.getLatitude()));
) { // Log.e(tag, "isEqual: " + coordinate.getLongitude().equals(element.getLongitude()));
Log.d("mapper", " cos sie zienilo "); boolean statement = coordinate.getLatitude().equals(element.getLatitude()) || coordinate.getLongitude().equals(element.getLongitude());
Marker marker = mapboxMap.addMarker(new MarkerViewOptions()
.title(cord.getUserId())
.position(new LatLng(cord.getLatitude(),cord.getLongitude())));
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position", Log.e(tag, "diff || diff: " + !statement);
new LatLngEvaluator(), marker.getPosition(), new LatLng(element.getLatitude(),element.getLongitude())); 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.setDuration(2000);
markerAnimator.start(); 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 { } else {
coordsMap.put(id,element); Log.e(tag, "Marker Added: " + id);
mapboxMap.addMarker(new MarkerOptions().position(new LatLng(element.getLatitude(), element.getLongitude()))); 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 @Override
public void onError(Throwable e) { public void onError(Throwable e) {
Log.e("Error",e.toString()); Log.e("Error", e.toString());
} }
}) })
); );
} }
private void configureLogoutButton(){ private void configureLogoutButton() {
// Logout button // Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton); 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 // Add the mapView lifecycle to the activity's lifecycle methods
@Override @Override
public void onResume() { public void onResume() {