MapActivity clean up, mapUtils created

This commit is contained in:
Adam Domagalski 2018-11-01 20:19:50 +01:00
parent fe13bee1f5
commit 1ae9217819
4 changed files with 56337 additions and 85 deletions

File diff suppressed because it is too large Load Diff

View File

@ -40,6 +40,7 @@ import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.network.RetrofitClientInstance; 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 com.uam.wmi.findmytutor.utils.mapUtils;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashMap; import java.util.HashMap;
@ -58,50 +59,44 @@ public class MapActivity extends BaseActivity
String tag = getClass().getName(); String tag = getClass().getName();
private MapView mapView;
private MapboxMap mapboxMap;
private int mInterval = 10000;
private Handler mHandler;
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();
private int mInterval = 10000;
private Handler mHandler = new Handler();
private Runnable mStatusChecker; private Runnable mStatusChecker;
private MapView mapView;
private MapboxMap mapboxMap;
private HashMap<String, Coordinate> coordsMap = new HashMap<>();
private HashMap<String, Marker> markerHash = new HashMap<>();
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
configureLogoutButton(); configureLogoutButton();
configureToggleMarkerLayerButton();
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() { // fetching coords service
@Override coordinateService = RetrofitClientInstance
public void run() { .createService(CoordinateService.class, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJhM2MxMDU1YS1kZDM0LTQ5ZWItYTFkNS0xY2E5YTE2YzY0ODgiLCJzdWIiOiJzdHJpbmciLCJqdGkiOiI4NTA0NDA5NS00NjBkLTQzZDgtYjMxMC0xYmNiNWMxNGExZjQiLCJleHAiOjE1NDMzNTAzMzQsImlzcyI6Imh0dHA6Ly9maW5kbXl0dXRvci5jb20iLCJhdWQiOiJodHRwOi8vZmluZG15dHV0b3IuY29tIn0.xGyu6iBeq9xF0ufBd01jNzILLq1NeYa-5MeVOiPahD8");
try { //
fetchTopCoords(); mStatusChecker = () -> {
} finally { try {
mHandler.postDelayed(mStatusChecker, mInterval); fetchTopCoords();
} } finally {
mHandler.postDelayed(mStatusChecker, mInterval);
} }
}; };
coordinateService = RetrofitClientInstance.createService(CoordinateService.class, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJhM2MxMDU1YS1kZDM0LTQ5ZWItYTFkNS0xY2E5YTE2YzY0ODgiLCJzdWIiOiJzdHJpbmciLCJqdGkiOiI4NTA0NDA5NS00NjBkLTQzZDgtYjMxMC0xYmNiNWMxNGExZjQiLCJleHAiOjE1NDMzNTAzMzQsImlzcyI6Imh0dHA6Ly9maW5kbXl0dXRvci5jb20iLCJhdWQiOiJodHRwOi8vZmluZG15dHV0b3IuY29tIn0.xGyu6iBeq9xF0ufBd01jNzILLq1NeYa-5MeVOiPahD8");
mHandler = new Handler();
Bundle extras = getIntent().getExtras();
Mapbox.getInstance(this, getString(R.string.access_token)); Mapbox.getInstance(this, getString(R.string.access_token));
mapView = findViewById(R.id.mapView);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState); mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this); mapView.getMapAsync(this);
} }
@ -113,34 +108,17 @@ public class MapActivity extends BaseActivity
.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(point -> {
@Override
public void onMapClick(@NonNull LatLng point) {
// When the user clicks on the map, we want to animate the marker to that // When the user clicks on the map, we want to animate the marker to that
// location. // location.
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position", ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
new LatLngEvaluator(), marker.getPosition(), point); new mapUtils.LatLngEvaluator(), marker.getPosition(), point);
markerAnimator.setDuration(2000); markerAnimator.setDuration(2000);
markerAnimator.start(); markerAnimator.start();
}
}); });
// TODO here we create static layer, we are able to hide/show (but we cannot put markers inthere)
VectorSource museums = new VectorSource("museums_source", "mapbox://mapbox.2opop9hr");
mapboxMap.addSource(museums);
CircleLayer museumsLayer = new CircleLayer("museums", "museums_source");
museumsLayer.setSourceLayer("museum-cusco");
museumsLayer.setProperties(
visibility(VISIBLE),
circleRadius(8f),
circleColor(Color.argb(255, 55, 148, 179))
);
mapboxMap.addLayer(museumsLayer);
// TODO what should happend on click? // TODO what should happend on click?
mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() { mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() {
@Override @Override
@ -153,22 +131,46 @@ public class MapActivity extends BaseActivity
}); });
setOnMapLongClickListener();
// addStaticLayer();
} }
private void setOnMapLongClickListener() {
mapboxMap.addOnMapLongClickListener((LatLng latLng) -> {
Log.e(tag, "eeeeee");
});
private static class LatLngEvaluator implements TypeEvaluator<LatLng> { }
// Method is used to interpolate the marker animation.
private LatLng latLng = new LatLng(); private void addStaticLayer() {
// Toggle layer button
final FloatingActionButton button = findViewById(R.id.toggleMarkerLayerButton);
button.setVisibility(View.VISIBLE);
@Override button.setOnClickListener(view -> {
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
latLng.setLatitude(startValue.getLatitude() Layer layer = mapboxMap.getLayer("museums");
+ ((endValue.getLatitude() - startValue.getLatitude()) * fraction)); if (layer != null) {
latLng.setLongitude(startValue.getLongitude() if (VISIBLE.equals(layer.getVisibility().getValue())) {
+ ((endValue.getLongitude() - startValue.getLongitude()) * fraction)); layer.setProperties(visibility(NONE));
return latLng; } else {
} layer.setProperties(visibility(VISIBLE));
}
}
});
// TODO here we create static layer, we are able to hide/show (but we cannot put markers inthere)
VectorSource museums = new VectorSource("museums_source", "mapbox://mapbox.2opop9hr");
mapboxMap.addSource(museums);
CircleLayer museumsLayer = new CircleLayer("museums", "museums_source");
museumsLayer.setSourceLayer("museum-cusco");
museumsLayer.setProperties(
visibility(VISIBLE),
circleRadius(8f),
circleColor(Color.argb(255, 55, 148, 179))
);
mapboxMap.addLayer(museumsLayer);
} }
private void fetchTopCoords() { private void fetchTopCoords() {
@ -201,7 +203,7 @@ public class MapActivity extends BaseActivity
if (!statement) { if (!statement) {
Log.e(tag, "replace and animate"); Log.e(tag, "replace and animate");
ValueAnimator markerAnimator = ObjectAnimator.ofObject(markerHash.get(id), "position", ValueAnimator markerAnimator = ObjectAnimator.ofObject(markerHash.get(id), "position",
new LatLngEvaluator(), markerHash.get(id).getPosition(), new mapUtils.LatLngEvaluator(), markerHash.get(id).getPosition(),
new LatLng(element.getLatitude(), element.getLongitude())); new LatLng(element.getLatitude(), element.getLongitude()));
markerAnimator.setDuration(2000); markerAnimator.setDuration(2000);
markerAnimator.start(); markerAnimator.start();
@ -251,23 +253,6 @@ public class MapActivity extends BaseActivity
}); });
} }
private void configureToggleMarkerLayerButton() {
// Toggle layer button
final FloatingActionButton button = findViewById(R.id.toggleMarkerLayerButton);
button.setOnClickListener(view -> {
Layer layer = mapboxMap.getLayer("museums");
if (layer != null) {
if (VISIBLE.equals(layer.getVisibility().getValue())) {
layer.setProperties(visibility(NONE));
} else {
layer.setProperties(visibility(VISIBLE));
}
}
});
}
// Add the mapView lifecycle to the activity's lifecycle methods // Add the mapView lifecycle to the activity's lifecycle methods
@Override @Override
@ -275,31 +260,26 @@ public class MapActivity extends BaseActivity
super.onResume(); super.onResume();
mapView.onResume(); mapView.onResume();
} }
@Override @Override
protected void onStart() { protected void onStart() {
super.onStart(); super.onStart();
mapView.onStart(); mapView.onStart();
} }
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
mapView.onStop(); mapView.onStop();
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
mapView.onPause(); mapView.onPause();
} }
@Override @Override
public void onLowMemory() { public void onLowMemory() {
super.onLowMemory(); super.onLowMemory();
mapView.onLowMemory(); mapView.onLowMemory();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -307,18 +287,15 @@ public class MapActivity extends BaseActivity
mHandler.removeCallbacks(mStatusChecker); mHandler.removeCallbacks(mStatusChecker);
disposable.dispose(); disposable.dispose();
} }
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState);
} }
@Override @Override
protected int getContentViewId() { protected int getContentViewId() {
return R.layout.activity_map; return R.layout.activity_map;
} }
@Override @Override
protected int getNavigationMenuItemId() { protected int getNavigationMenuItemId() {
return R.id.nav_map; return R.id.nav_map;

View File

@ -0,0 +1,44 @@
package com.uam.wmi.findmytutor.utils;
import android.animation.TypeEvaluator;
import android.content.Context;
import com.mapbox.mapboxsdk.geometry.LatLng;
import java.io.IOException;
import java.io.InputStream;
public class mapUtils {
public static class LatLngEvaluator implements TypeEvaluator<LatLng> {
// Method is used to interpolate the marker animation.
private LatLng latLng = new LatLng();
@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
latLng.setLatitude(startValue.getLatitude()
+ ((endValue.getLatitude() - startValue.getLatitude()) * fraction));
latLng.setLongitude(startValue.getLongitude()
+ ((endValue.getLongitude() - startValue.getLongitude()) * fraction));
return latLng;
}
}
public static String loadJsonFromAsset(Context context, String filename) {
// Using this method to load in GeoJSON files from the assets folder.
try {
InputStream is = context.getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}

View File

@ -23,6 +23,7 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical"> android:orientation="vertical">
<android.support.design.widget.FloatingActionButton <android.support.design.widget.FloatingActionButton
@ -42,6 +43,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:clickable="true" android:clickable="true"
android:visibility="gone"
app:backgroundTint="@color/msg_no_notes" app:backgroundTint="@color/msg_no_notes"
app:layout_constraintTop_toBottomOf="@+id/logoutButton" app:layout_constraintTop_toBottomOf="@+id/logoutButton"
mapbox:srcCompat="@android:drawable/ic_dialog_map" mapbox:srcCompat="@android:drawable/ic_dialog_map"