Skip to content

Commit 6dec691

Browse files
authored
[Enhancement] Add option to centre on GPS location for issue commons-app#5008 (commons-app#5098)
* Add option to centre on GPS location for issue commons-app#5008 * Rename and add new comments for issue commons-app#5008 * Add new comments for issue commons-app#5008 * Update comments for issue commons-app#5008
1 parent 7b18c3e commit 6dec691

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

app/src/main/java/fr/free/nrw/commons/LocationPicker/LocationPickerActivity.java

+42
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import android.content.Intent;
1313
import android.graphics.BitmapFactory;
14+
import android.location.Location;
1415
import android.os.Bundle;
1516
import android.text.Html;
1617
import android.text.method.LinkMovementMethod;
@@ -20,6 +21,7 @@
2021
import android.widget.Button;
2122
import android.widget.ImageView;
2223
import android.widget.TextView;
24+
import android.widget.Toast;
2325
import androidx.annotation.NonNull;
2426
import androidx.annotation.Nullable;
2527
import androidx.appcompat.app.ActionBar;
@@ -29,6 +31,8 @@
2931
import androidx.lifecycle.Observer;
3032
import androidx.lifecycle.ViewModelProvider;
3133
import com.google.android.material.floatingactionbutton.FloatingActionButton;
34+
import com.mapbox.android.core.location.LocationEngineCallback;
35+
import com.mapbox.android.core.location.LocationEngineResult;
3236
import com.mapbox.android.core.permissions.PermissionsManager;
3337
import com.mapbox.geojson.Point;
3438
import com.mapbox.mapboxsdk.camera.CameraPosition;
@@ -92,6 +96,10 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
9296
* activity : activity key
9397
*/
9498
private String activity;
99+
/**
100+
* location : location
101+
*/
102+
private Location location;
95103
/**
96104
* modifyLocationButton : button for start editing location
97105
*/
@@ -104,6 +112,10 @@ public class LocationPickerActivity extends BaseActivity implements OnMapReadyCa
104112
* placeSelectedButton : fab for selecting location
105113
*/
106114
FloatingActionButton placeSelectedButton;
115+
/**
116+
* fabCenterOnLocation: button for center on location;
117+
*/
118+
FloatingActionButton fabCenterOnLocation;
107119
/**
108120
* droppedMarkerLayer : Layer for static screen
109121
*/
@@ -154,6 +166,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
154166
addPlaceSelectedButton();
155167
addCredits();
156168
getToolbarUI();
169+
addCenterOnGPSButton();
157170

158171
if ("UploadActivity".equals(activity)) {
159172
placeSelectedButton.setVisibility(View.GONE);
@@ -162,6 +175,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
162175
largeToolbarText.setText(getResources().getString(R.string.image_location));
163176
smallToolbarText.setText(getResources().
164177
getString(R.string.check_whether_location_is_correct));
178+
fabCenterOnLocation.setVisibility(View.GONE);
165179
}
166180

167181
mapView.onCreate(savedInstanceState);
@@ -275,6 +289,7 @@ private void onClickModifyLocation() {
275289
largeToolbarText.setText(getResources().getString(R.string.choose_a_location));
276290
smallToolbarText.setText(getResources().getString(R.string.pan_and_zoom_to_adjust));
277291
bindListeners();
292+
fabCenterOnLocation.setVisibility(View.VISIBLE);
278293
}
279294

280295
/**
@@ -339,6 +354,20 @@ private void enableLocationComponent(@NonNull final Style loadedMapStyle) {
339354
// Set the component's render mode
340355
locationComponent.setRenderMode(RenderMode.NORMAL);
341356

357+
// Get the component's location engine to receive user's last location
358+
locationComponent.getLocationEngine().getLastLocation(
359+
new LocationEngineCallback<LocationEngineResult>() {
360+
@Override
361+
public void onSuccess(LocationEngineResult result) {
362+
location = result.getLastLocation();
363+
}
364+
365+
@Override
366+
public void onFailure(@NonNull Exception exception) {
367+
}
368+
});
369+
370+
342371
}
343372
}
344373

@@ -405,6 +434,19 @@ void placeSelected() {
405434
setResult(AppCompatActivity.RESULT_OK, returningIntent);
406435
finish();
407436
}
437+
/**
438+
* Center the camera on the last saved location
439+
*/
440+
private void addCenterOnGPSButton(){
441+
fabCenterOnLocation = findViewById(R.id.center_on_gps);
442+
fabCenterOnLocation.setOnClickListener(view -> getCenter());
443+
}
444+
/**
445+
* Animate map to move to desired Latitude and Longitude
446+
*/
447+
void getCenter() {
448+
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()),15.0));
449+
}
408450

409451
@Override
410452
protected void onStart() {

app/src/main/res/layout/bottom_container_location_picker.xml

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
67
android:layout_gravity="bottom">
@@ -19,6 +20,19 @@
1920
app:layout_constraintEnd_toEndOf="parent"
2021
app:srcCompat="@drawable/ic_check_black_24dp" />
2122

23+
<com.google.android.material.floatingactionbutton.FloatingActionButton
24+
android:id="@+id/center_on_gps"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_marginBottom="20dp"
28+
android:contentDescription="@string/select_location_location_picker"
29+
app:backgroundTint="@color/white"
30+
app:elevation="3dp"
31+
app:layout_anchorGravity="bottom|end"
32+
app:layout_constraintBottom_toTopOf="@+id/location_chosen_button"
33+
app:layout_constraintEnd_toEndOf="@id/location_chosen_button"
34+
app:srcCompat="@drawable/ic_my_location_black_24dp" />
35+
2236
<androidx.appcompat.widget.AppCompatTextView
2337
android:id="@+id/tv_attribution"
2438
android:layout_width="wrap_content"

0 commit comments

Comments
 (0)