11package org .nitri .opentopo ;
22
3+ import android .Manifest ;
34import android .annotation .SuppressLint ;
45import android .content .Context ;
56import android .content .SharedPreferences ;
7+ import android .content .pm .PackageManager ;
68import android .graphics .Bitmap ;
79import android .graphics .BitmapFactory ;
810import android .location .Location ;
911import android .location .LocationListener ;
1012import android .location .LocationManager ;
13+ import android .os .Build ;
1114import android .os .Bundle ;
1215import android .os .Handler ;
16+
1317import androidx .annotation .NonNull ;
1418import androidx .annotation .Nullable ;
1519import androidx .fragment .app .Fragment ;
3438
3539import org .nitri .opentopo .nearby .entity .NearbyItem ;
3640import org .nitri .opentopo .overlay .OverlayHelper ;
41+ import org .osmdroid .api .IGeoPoint ;
3742import org .osmdroid .config .Configuration ;
3843import org .osmdroid .config .IConfigurationProvider ;
3944import org .osmdroid .events .DelayedMapListener ;
@@ -102,6 +107,9 @@ public boolean onZoom(ZoomEvent event) {
102107 private final static String PARAM_LATITUDE = "latitude" ;
103108 private final static String PARAM_LONGITUDE = "longitude" ;
104109
110+ private final static String STATE_LATITUDE = "latitude" ;
111+ private final static String STATE_LONGITUDE = "longitude" ;
112+
105113 private SharedPreferences mPrefs ;
106114 private static final String MAP_PREFS = "map_prefs" ;
107115
@@ -117,6 +125,7 @@ public boolean onZoom(ZoomEvent event) {
117125 private static final String TAG = MapFragment .class .getSimpleName ();
118126 private TextView mCopyRightView ;
119127 private int mOverlay = OverlayHelper .OVERLAY_NONE ;
128+ private GeoPoint mMapCenterState ;
120129
121130 public MapFragment () {
122131 // Required empty public constructor
@@ -144,10 +153,19 @@ public void onCreate(Bundle savedInstanceState) {
144153 IConfigurationProvider configuration = Configuration .getInstance ();
145154 configuration .setUserAgentValue (BuildConfig .APPLICATION_ID );
146155 configuration .load (context , PreferenceManager .getDefaultSharedPreferences (context ));
147- if (getActivity () != null ) {
148- mPrefs = getActivity ().getSharedPreferences (MAP_PREFS , Context .MODE_PRIVATE );
149- mBaseMap = mPrefs .getInt (PREF_BASE_MAP , BASE_MAP_OTM );
150- mOverlay = mPrefs .getInt (PREF_OVERLAY , OverlayHelper .OVERLAY_NONE );
156+ mPrefs = requireActivity ().getSharedPreferences (MAP_PREFS , Context .MODE_PRIVATE );
157+ mBaseMap = mPrefs .getInt (PREF_BASE_MAP , BASE_MAP_OTM );
158+ mOverlay = mPrefs .getInt (PREF_OVERLAY , OverlayHelper .OVERLAY_NONE );
159+ mLocationManager = (LocationManager ) requireActivity ().getSystemService (Context .LOCATION_SERVICE );
160+ }
161+
162+
163+ @ Override
164+ public void onActivityCreated (@ Nullable Bundle savedInstanceState ) {
165+ super .onActivityCreated (savedInstanceState );
166+ if (savedInstanceState != null ) {
167+ mMapCenterState = new GeoPoint (savedInstanceState .getDouble (STATE_LATITUDE , 0 ),
168+ savedInstanceState .getDouble (STATE_LONGITUDE , 0 ));
151169 }
152170 }
153171
@@ -190,14 +208,18 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
190208 mMapView .getOverlays ().add (this .mCompassOverlay );
191209 mMapView .getOverlays ().add (this .mScaleBarOverlay );
192210
211+ if (mMapCenterState != null ) {
212+ mMapView .getController ().setCenter (mMapCenterState );
213+ }
214+
193215 mMapView .addMapListener (new DelayedMapListener (mDragListener ));
194216
195217 mCopyRightView = view .findViewById (R .id .copyrightView );
196218
197219 setBaseMap ();
198220
199221 mLocationOverlay .enableMyLocation ();
200- mLocationOverlay .enableFollowLocation ();
222+ mLocationOverlay .disableFollowLocation ();
201223 mLocationOverlay .setOptionsMenuEnabled (true );
202224 mCompassOverlay .enableCompass ();
203225 mMapView .setVisibility (View .VISIBLE );
@@ -226,6 +248,19 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
226248 if (mListener .getSelectedNearbyPlace () != null ) {
227249 showNearbyPlace (mListener .getSelectedNearbyPlace ());
228250 }
251+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
252+ if (requireActivity ().checkSelfPermission (Manifest .permission .ACCESS_FINE_LOCATION ) == PackageManager .PERMISSION_GRANTED ||
253+ requireActivity ().checkSelfPermission (Manifest .permission .ACCESS_COARSE_LOCATION ) == PackageManager .PERMISSION_GRANTED ) {
254+ mCurrentLocation = mLocationManager .getLastKnownLocation (LocationManager .PASSIVE_PROVIDER );
255+ }
256+ } else {
257+ mCurrentLocation = mLocationManager .getLastKnownLocation (LocationManager .PASSIVE_PROVIDER );
258+ }
259+ if (mMapCenterState != null ) {
260+ mMapView .getController ().setCenter (mMapCenterState );
261+ } else if (mCurrentLocation != null ) {
262+ mMapView .getController ().setCenter (new GeoPoint (mCurrentLocation .getLatitude (), mCurrentLocation .getLongitude ()));
263+ }
229264
230265 }
231266
@@ -315,6 +350,28 @@ private void disableFollow() {
315350 mMapHandler .removeCallbacks (mCenterRunnable );
316351 }
317352
353+ @ SuppressLint ("MissingPermission" )
354+ @ Override
355+ public void onResume () {
356+ super .onResume ();
357+ //File basePath = Configuration.getInstance().getOsmdroidBasePath();
358+ //File cache = Configuration.getInstance().getOsmdroidTileCache();
359+ initMap ();
360+ if (mLocationManager != null ) {
361+ try {
362+ mLocationManager .requestLocationUpdates (LocationManager .GPS_PROVIDER , 0L , 0f , this );
363+ } catch (Exception ex ) {
364+ ex .printStackTrace ();
365+
366+ }
367+ try {
368+ mLocationManager .requestLocationUpdates (LocationManager .NETWORK_PROVIDER , 0L , 0f , this );
369+ } catch (Exception ex ) {
370+ ex .printStackTrace ();
371+ }
372+ }
373+ }
374+
318375 @ Override
319376 public void onPause () {
320377 super .onPause ();
@@ -332,28 +389,13 @@ public void onPause() {
332389 mScaleBarOverlay .disableScaleBar ();
333390 }
334391
335- @ SuppressLint ("MissingPermission" )
336392 @ Override
337- public void onResume () {
338- super .onResume ();
339- //File basePath = Configuration.getInstance().getOsmdroidBasePath();
340- //File cache = Configuration.getInstance().getOsmdroidTileCache();
341- initMap ();
342- if (getActivity () != null ) {
343- mLocationManager = (LocationManager ) getActivity ().getSystemService (Context .LOCATION_SERVICE );
344- if (mLocationManager != null ) {
345- try {
346- mLocationManager .requestLocationUpdates (LocationManager .GPS_PROVIDER , 0L , 0f , this );
347- } catch (Exception ex ) {
348- ex .printStackTrace ();
349-
350- }
351- try {
352- mLocationManager .requestLocationUpdates (LocationManager .NETWORK_PROVIDER , 0L , 0f , this );
353- } catch (Exception ex ) {
354- ex .printStackTrace ();
355- }
356- }
393+ public void onSaveInstanceState (@ NonNull Bundle outState ) {
394+ super .onSaveInstanceState (outState );
395+ if (mMapView != null ) {
396+ mMapCenterState = (GeoPoint ) mMapView .getMapCenter ();
397+ outState .putDouble (STATE_LATITUDE , mMapCenterState .getLatitude ());
398+ outState .putDouble (STATE_LONGITUDE , mMapCenterState .getLongitudeE6 ());
357399 }
358400 }
359401
@@ -652,5 +694,6 @@ public interface OnFragmentInteractionListener {
652694 */
653695 NearbyItem getSelectedNearbyPlace ();
654696 }
697+
655698}
656699
0 commit comments