Compare commits

...

1 Commits

Author SHA1 Message Date
Adam Domagalski
2cd51e9920 hovering marker 2018-11-26 21:34:10 +01:00
4 changed files with 104 additions and 9 deletions

View File

@ -30,6 +30,7 @@ repositories {
} }
dependencies { dependencies {
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-places:0.6.0'
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:preference-v7:27.1.1' implementation 'com.android.support:preference-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:appcompat-v7:27.1.1'

View File

@ -4,16 +4,21 @@ import android.Manifest;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.animation.ValueAnimator; import android.animation.ValueAnimator;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.graphics.PointF;
import android.location.Location; import android.location.Location;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.util.Log; import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator; import android.view.animation.LinearInterpolator;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -22,6 +27,7 @@ import com.getbase.floatingactionbutton.FloatingActionButton;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException; import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.mapbox.android.core.permissions.PermissionsListener; import com.mapbox.android.core.permissions.PermissionsListener;
import com.mapbox.android.core.permissions.PermissionsManager; import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory; import com.mapbox.mapboxsdk.annotations.IconFactory;
@ -74,12 +80,15 @@ public class MapActivity extends BaseActivity
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private Runnable mStatusChecker; private Runnable mStatusChecker;
private Handler manualLocHandler = new Handler(); private Handler manualLocHandler = new Handler();
private ImageView hoveringMarkerView;
private Runnable manualLocStatusChecker; private Runnable manualLocStatusChecker;
private MapView mapView; private MapView mapView;
private MapboxMap mapboxMap; private MapboxMap mapboxMap;
private Button selectLocationButton; private Button selectLocationButton;
private Button removeLocationButton; private Button removeLocationButton;
private Button hoverSelectLocationButton;
private Marker tmpLocalMarker; private Marker tmpLocalMarker;
private Marker droppedMarker;
private Coordinate droppedMarkercoordinate; private Coordinate droppedMarkercoordinate;
private HashMap<String, Coordinate> coordsMap = new HashMap<>(); private HashMap<String, Coordinate> coordsMap = new HashMap<>();
private HashMap<String, Marker> markerHash = new HashMap<>(); private HashMap<String, Marker> markerHash = new HashMap<>();
@ -157,10 +166,81 @@ public class MapActivity extends BaseActivity
setToggleMapBoundsArea(); setToggleMapBoundsArea();
setHoveringMarker();
if (PrefUtils.getIsTutor(this)){ if (PrefUtils.getIsTutor(this)){
setOnMapLongClickListener(); setOnMapLongClickListener();
} }
// addStaticLayer(); // addStaticLayer();
}
// TODO:
private void setHoveringMarker() {
// When user is still picking a location, we hover a marker above the mapboxMap in the center.
// This is done by using an image view with the default marker found in the SDK. You can
// swap out for your own marker image, just make sure it matches up with the dropped marker.
hoveringMarkerView = new ImageView(this);
hoveringMarkerView.setImageResource(R.drawable.green_marker);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
hoveringMarkerView.setLayoutParams(params);
mapView.addView(hoveringMarkerView); // somewhere esle
hoverSelectLocationButton = findViewById(R.id.drop_hovering_location_button);
hoverSelectLocationButton.setVisibility(View.VISIBLE);
hoveringMarkerView.setVisibility(View.VISIBLE);
selectLocationButton.setVisibility(View.GONE);
removeLocationButton.setVisibility(View.GONE);
hoverSelectLocationButton.setOnClickListener(view -> {
if (mapboxMap != null) {
if (droppedMarker == null) {
// We first find where the hovering marker position is relative to the mapboxMap.
// Then we set the visibility to gone.
float coordinateX = hoveringMarkerView.getLeft() + (hoveringMarkerView.getWidth() / 2);
float coordinateY = hoveringMarkerView.getBottom() - (hoveringMarkerView.getHeight() / 2);
float[] coords = new float[]{coordinateX, coordinateY};
final Point latLng = Point.fromLngLat(mapboxMap.getProjection().fromScreenLocation(
new PointF(coords[0], coords[1])).getLongitude(), mapboxMap.getProjection().fromScreenLocation(
new PointF(coords[0], coords[1])).getLatitude());
hoveringMarkerView.setVisibility(View.GONE);
// Transform the appearance of the button to become the cancel button
hoverSelectLocationButton.setBackgroundColor(
ContextCompat.getColor(MapActivity.this, R.color.colorAccent));
hoverSelectLocationButton.setText("Drop my marker");
// Create the marker icon the dropped marker will be using.
Icon icon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.blue_marker);
// Placing the marker on the mapboxMap as soon as possible causes the illusion
// that the hovering marker and dropped marker are the same.
droppedMarker = mapboxMap.addMarker(new MarkerOptions().setIcon(icon)
.position(new LatLng(latLng.latitude(), latLng.longitude())));
PrefUtils.putManualLocation(this, new LatLng(latLng.latitude(), latLng.longitude()));
handleBackgroundTaskLifeCycle();
}
// else {
// // When the marker is dropped, the user has clicked the button to cancel.
// // Therefore, we pick the marker back up.
// mapboxMap.removeMarker(droppedMarker);
//
// // Switch the button apperance back to select a location.
// hoverSelectLocationButton.setBackgroundColor(
// ContextCompat.getColor(MapActivity.this, R.color.colorPrimary));
// hoverSelectLocationButton.setText("Pick Up ");
//
// // Lastly, set the hovering marker back to visible.
// hoveringMarkerView.setVisibility(View.VISIBLE);
// droppedMarker = null;
// }
}
});
} }
private void setToggleMapBoundsArea() { private void setToggleMapBoundsArea() {
@ -258,15 +338,15 @@ public class MapActivity extends BaseActivity
// Toast instructing user to tap on the mapboxMap // Toast instructing user to tap on the mapboxMap
// TODO PUT MANUAL CORD // TODO PUT MANUAL CORD
try { try {
droppedMarkercoordinate = new Coordinate( // droppedMarkercoordinate = new Coordinate(
latLng.getLatitude(), // latLng.getLatitude(),
latLng.getLongitude(), // latLng.getLongitude(),
latLng.getAltitude(), // latLng.getAltitude(),
"approx", // "approx",
PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()), // PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()),
PrefUtils.getUserId(getApplicationContext()), // PrefUtils.getUserId(getApplicationContext()),
PrefUtils.getLocationLevel(getApplicationContext()) // PrefUtils.getLocationLevel(getApplicationContext())
); // );
PrefUtils.putManualLocation(this, latLng); PrefUtils.putManualLocation(this, latLng);
handleBackgroundTaskLifeCycle(); handleBackgroundTaskLifeCycle();

View File

@ -111,6 +111,19 @@
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:visibility="gone" /> android:visibility="gone" />
<Button
android:id="@+id/drop_hovering_location_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:background="@color/half_black"
android:text="@string/drop_my_marker"
android:textColor="@android:color/white"
android:visibility="gone" />
<Button <Button
android:id="@+id/remove_location_button" android:id="@+id/remove_location_button"
android:layout_width="fill_parent" android:layout_width="fill_parent"

View File

@ -213,6 +213,7 @@
<string name="map_activity_label" translatable="false">MapActivity</string> <string name="map_activity_label" translatable="false">MapActivity</string>
<string name="nav_profile">User profile</string> <string name="nav_profile">User profile</string>
<string name="remove_manual_location">Remove Manual location</string> <string name="remove_manual_location">Remove Manual location</string>
<string name="drop_my_marker">Drop my marker</string>
</resources> </resources>