4141
4242import org .nitri .opentopo .model .LocationViewModel ;
4343import org .nitri .opentopo .nearby .entity .NearbyItem ;
44+ import org .nitri .opentopo .overlay .GestureOverlay ;
4445import org .nitri .opentopo .overlay .OverlayHelper ;
4546import org .osmdroid .config .Configuration ;
4647import org .osmdroid .config .IConfigurationProvider ;
6768import io .ticofab .androidgpxparser .parser .domain .Gpx ;
6869
6970
70- public class MapFragment extends Fragment implements LocationListener , PopupMenu .OnMenuItemClickListener {
71+ public class MapFragment extends Fragment implements LocationListener , PopupMenu .OnMenuItemClickListener ,
72+ GestureOverlay .GestureCallback {
7173
7274 private MapView mMapView ;
7375 private MyLocationNewOverlay mLocationOverlay ;
@@ -88,18 +90,27 @@ public void run() {
8890 }
8991 };
9092
93+ private final Runnable mEnableFollowRunnable = new Runnable () {
94+
95+ @ Override
96+ public void run () {
97+ if (mLocationOverlay != null ) {
98+ mLocationOverlay .enableFollowLocation ();
99+ mLocationOverlay .setEnableAutoStop (true );
100+ }
101+ mMapHandler .postDelayed (this , 5000 );
102+ }
103+ };
104+
91105 private final MapListener mDragListener = new MapListener () {
92106 @ Override
93107 public boolean onScroll (ScrollEvent event ) {
94- if (mFollow && mMapHandler != null ) {
95- mMapHandler .removeCallbacks (mCenterRunnable );
96- mMapHandler .postDelayed (mCenterRunnable , 6000 );
97- }
98- return true ;
108+ return false ;
99109 }
100110
101111 @ Override
102112 public boolean onZoom (ZoomEvent event ) {
113+ onUserMapInteraction ();
103114 return false ;
104115 }
105116 };
@@ -140,6 +151,7 @@ public boolean onZoom(ZoomEvent event) {
140151 private int mLastNearbyAnimateToId ;
141152
142153 private LocationViewModel mLocationViewModel ;
154+ private GestureOverlay mGestureOverlay ;
143155
144156 public MapFragment () {
145157 }
@@ -242,6 +254,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
242254 mRotationGestureOverlay = new RotationGestureOverlay (mMapView );
243255 mRotationGestureOverlay .setEnabled (true );
244256
257+ mGestureOverlay = new GestureOverlay (this );
258+ mMapView .getOverlays ().add (mGestureOverlay );
259+
245260 mMapView .setMaxZoomLevel (17d );
246261 mMapView .setTilesScaledToDpi (true );
247262 if (mListener != null && mListener .isFullscreen ()) {
@@ -269,6 +284,21 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
269284 mOverlayHelper = new OverlayHelper (getActivity (), mMapView );
270285
271286 setTilesOverlay ();
287+
288+ mLocationOverlay .runOnFirstFix (new Runnable () {
289+ @ Override
290+ public void run () {
291+ final GeoPoint location = mLocationOverlay .getMyLocation ();
292+ if (location != null ) {
293+ try {
294+ requireActivity ().runOnUiThread (() -> mMapView .getController ().animateTo (location ));
295+ } catch (IllegalStateException e ) {
296+ e .printStackTrace ();
297+ }
298+ }
299+ ;
300+ }
301+ });
272302 }
273303
274304 return view ;
@@ -424,8 +454,9 @@ private void enableFollow() {
424454 if (getActivity () != null )
425455 ((AppCompatActivity ) getActivity ()).supportInvalidateOptionsMenu ();
426456 mLocationOverlay .enableFollowLocation ();
457+ mLocationOverlay .setEnableAutoStop (true );
427458 mMapHandler .removeCallbacks (mCenterRunnable );
428- mMapHandler .post (mCenterRunnable );
459+ // mMapHandler.post(mCenterRunnable);
429460 mPrefs .edit ().putBoolean (PREF_FOLLOW , true ).apply ();
430461 }
431462
@@ -434,7 +465,7 @@ private void disableFollow() {
434465 if (getActivity () != null )
435466 ((AppCompatActivity ) getActivity ()).supportInvalidateOptionsMenu ();
436467 mLocationOverlay .disableFollowLocation ();
437- mMapHandler .removeCallbacks ( mCenterRunnable );
468+ mMapHandler .removeCallbacksAndMessages ( null );
438469 mPrefs .edit ().putBoolean (PREF_FOLLOW , false ).apply ();
439470 }
440471
@@ -771,7 +802,8 @@ public boolean onMenuItemClick(MenuItem menuItem) {
771802
772803 @ Override
773804 public void onLocationChanged (Location location ) {
774- if (BuildConfig .DEBUG && location != null ) Log .d (TAG , String .format ("Location: %f, %f" , location .getLatitude (), location .getLongitude ()));
805+ if (BuildConfig .DEBUG && location != null )
806+ Log .d (TAG , String .format ("Location: %f, %f" , location .getLatitude (), location .getLongitude ()));
775807 mLocationViewModel .getCurrentLocation ().setValue (location );
776808 }
777809
@@ -811,11 +843,21 @@ public void onDestroy() {
811843 mCompassOverlay = null ;
812844 mScaleBarOverlay = null ;
813845 mRotationGestureOverlay = null ;
846+ mGestureOverlay = null ;
814847 if (mOverlayHelper != null )
815848 mOverlayHelper .destroy ();
816849 mMapView = null ;
817850 }
818851
852+ @ Override
853+ public void onUserMapInteraction () {
854+ if (mFollow ) {
855+ // follow disabled by gesture -> re-enable with delay
856+ mMapHandler .removeCallbacksAndMessages (null );
857+ mMapHandler .postDelayed (mEnableFollowRunnable , 5000 );
858+ }
859+ }
860+
819861 public interface OnFragmentInteractionListener {
820862
821863 /**
0 commit comments