[Enhancement] Add option to centre on GPS location for issue #5008 (#5098)

* Add option to centre on GPS location for issue #5008

* Rename and add new comments for issue #5008

* Add new comments for issue #5008

* Update comments for issue #5008
This commit is contained in:
russianbear222 2022-12-17 21:05:45 +07:00 committed by GitHub
parent 7b18c3eeb7
commit 6dec69147f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View file

@ -11,6 +11,7 @@ import static fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
@ -20,6 +21,7 @@ import android.view.animation.OvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
@ -29,6 +31,8 @@ import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.mapbox.android.core.location.LocationEngineCallback;
import com.mapbox.android.core.location.LocationEngineResult;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.camera.CameraPosition;
@ -92,6 +96,10 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
* activity : activity key
*/
private String activity;
/**
* location : location
*/
private Location location;
/**
* modifyLocationButton : button for start editing location
*/
@ -104,6 +112,10 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
* placeSelectedButton : fab for selecting location
*/
FloatingActionButton placeSelectedButton;
/**
* fabCenterOnLocation: button for center on location;
*/
FloatingActionButton fabCenterOnLocation;
/**
* droppedMarkerLayer : Layer for static screen
*/
@ -154,6 +166,7 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
addPlaceSelectedButton();
addCredits();
getToolbarUI();
addCenterOnGPSButton();
if ("UploadActivity".equals(activity)) {
placeSelectedButton.setVisibility(View.GONE);
@ -162,6 +175,7 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
largeToolbarText.setText(getResources().getString(R.string.image_location));
smallToolbarText.setText(getResources().
getString(R.string.check_whether_location_is_correct));
fabCenterOnLocation.setVisibility(View.GONE);
}
mapView.onCreate(savedInstanceState);
@ -275,6 +289,7 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
largeToolbarText.setText(getResources().getString(R.string.choose_a_location));
smallToolbarText.setText(getResources().getString(R.string.pan_and_zoom_to_adjust));
bindListeners();
fabCenterOnLocation.setVisibility(View.VISIBLE);
}
/**
@ -339,6 +354,20 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
// Set the component's render mode
locationComponent.setRenderMode(RenderMode.NORMAL);
// Get the component's location engine to receive user's last location
locationComponent.getLocationEngine().getLastLocation(
new LocationEngineCallback<LocationEngineResult>() {
@Override
public void onSuccess(LocationEngineResult result) {
location = result.getLastLocation();
}
@Override
public void onFailure(@NonNull Exception exception) {
}
});
}
}
@ -405,6 +434,19 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
setResult(AppCompatActivity.RESULT_OK, returningIntent);
finish();
}
/**
* Center the camera on the last saved location
*/
private void addCenterOnGPSButton(){
fabCenterOnLocation = findViewById(R.id.center_on_gps);
fabCenterOnLocation.setOnClickListener(view -> getCenter());
}
/**
* Animate map to move to desired Latitude and Longitude
*/
void getCenter() {
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),15.0));
}
@Override
protected void onStart() {

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">
@ -19,6 +20,19 @@
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_check_black_24dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/center_on_gps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:contentDescription="@string/select_location_location_picker"
app:backgroundTint="@color/white"
app:elevation="3dp"
app:layout_anchorGravity="bottom|end"
app:layout_constraintBottom_toTopOf="@+id/location_chosen_button"
app:layout_constraintEnd_toEndOf="@id/location_chosen_button"
app:srcCompat="@drawable/ic_my_location_black_24dp" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tv_attribution"
android:layout_width="wrap_content"