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.service.CoordinateService;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.mapUtils;
import java.nio.ByteBuffer;
import java.util.HashMap;
@ -58,50 +59,44 @@ public class MapActivity extends BaseActivity
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 CompositeDisposable disposable = new CompositeDisposable();
private int mInterval = 10000;
private Handler mHandler = new Handler();
private Runnable mStatusChecker;
private MapView mapView;
private MapboxMap mapboxMap;
private HashMap<String, Coordinate> coordsMap = new HashMap<>();
private HashMap<String, Marker> markerHash = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
configureLogoutButton();
configureToggleMarkerLayerButton();
final SharedPreferences sharedPref = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
final String authToken = sharedPref.getString("authToken", null);
mStatusChecker = new Runnable() {
@Override
public void run() {
try {
fetchTopCoords();
} finally {
mHandler.postDelayed(mStatusChecker, mInterval);
}
// fetching coords service
coordinateService = RetrofitClientInstance
.createService(CoordinateService.class, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiJhM2MxMDU1YS1kZDM0LTQ5ZWItYTFkNS0xY2E5YTE2YzY0ODgiLCJzdWIiOiJzdHJpbmciLCJqdGkiOiI4NTA0NDA5NS00NjBkLTQzZDgtYjMxMC0xYmNiNWMxNGExZjQiLCJleHAiOjE1NDMzNTAzMzQsImlzcyI6Imh0dHA6Ly9maW5kbXl0dXRvci5jb20iLCJhdWQiOiJodHRwOi8vZmluZG15dHV0b3IuY29tIn0.xGyu6iBeq9xF0ufBd01jNzILLq1NeYa-5MeVOiPahD8");
//
mStatusChecker = () -> {
try {
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));
mapView = (MapView) findViewById(R.id.mapView);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@ -113,34 +108,17 @@ public class MapActivity extends BaseActivity
.position(new LatLng(52.466782, 16.927549)));
mStatusChecker.run();
mapboxMap.addOnMapClickListener(new MapboxMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
mapboxMap.addOnMapClickListener(point -> {
// When the user clicks on the map, we want to animate the marker to that
// location.
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
new LatLngEvaluator(), marker.getPosition(), point);
markerAnimator.setDuration(2000);
markerAnimator.start();
}
// When the user clicks on the map, we want to animate the marker to that
// location.
ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position",
new mapUtils.LatLngEvaluator(), marker.getPosition(), point);
markerAnimator.setDuration(2000);
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?
mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() {
@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
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;
}
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));
}
}
});
// 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() {
@ -201,7 +203,7 @@ public class MapActivity extends BaseActivity
if (!statement) {
Log.e(tag, "replace and animate");
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()));
markerAnimator.setDuration(2000);
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
@Override
@ -275,31 +260,26 @@ public class MapActivity extends BaseActivity
super.onResume();
mapView.onResume();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
@ -307,18 +287,15 @@ public class MapActivity extends BaseActivity
mHandler.removeCallbacks(mStatusChecker);
disposable.dispose();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
protected int getContentViewId() {
return R.layout.activity_map;
}
@Override
protected int getNavigationMenuItemId() {
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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<android.support.design.widget.FloatingActionButton
@ -42,6 +43,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:clickable="true"
android:visibility="gone"
app:backgroundTint="@color/msg_no_notes"
app:layout_constraintTop_toBottomOf="@+id/logoutButton"
mapbox:srcCompat="@android:drawable/ic_dialog_map"