Skip to content

Commit

Permalink
Fix #6: Stable location on app start and configuration change
Browse files Browse the repository at this point in the history
  • Loading branch information
Pygmalion69 committed Aug 2, 2019
1 parent c7a1f10 commit 1685aaf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "org.nitri.opentopo"
minSdkVersion 19
targetSdkVersion 28
versionCode 14
versionName "1.6.1"
versionCode 15
versionName "1.7"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
95 changes: 69 additions & 26 deletions app/src/main/java/org/nitri/opentopo/MapFragment.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.nitri.opentopo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand All @@ -34,6 +38,7 @@

import org.nitri.opentopo.nearby.entity.NearbyItem;
import org.nitri.opentopo.overlay.OverlayHelper;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.config.Configuration;
import org.osmdroid.config.IConfigurationProvider;
import org.osmdroid.events.DelayedMapListener;
Expand Down Expand Up @@ -102,6 +107,9 @@ public boolean onZoom(ZoomEvent event) {
private final static String PARAM_LATITUDE = "latitude";
private final static String PARAM_LONGITUDE = "longitude";

private final static String STATE_LATITUDE = "latitude";
private final static String STATE_LONGITUDE = "longitude";

private SharedPreferences mPrefs;
private static final String MAP_PREFS = "map_prefs";

Expand All @@ -117,6 +125,7 @@ public boolean onZoom(ZoomEvent event) {
private static final String TAG = MapFragment.class.getSimpleName();
private TextView mCopyRightView;
private int mOverlay = OverlayHelper.OVERLAY_NONE;
private GeoPoint mMapCenterState;

public MapFragment() {
// Required empty public constructor
Expand Down Expand Up @@ -144,10 +153,19 @@ public void onCreate(Bundle savedInstanceState) {
IConfigurationProvider configuration = Configuration.getInstance();
configuration.setUserAgentValue(BuildConfig.APPLICATION_ID);
configuration.load(context, PreferenceManager.getDefaultSharedPreferences(context));
if (getActivity() != null) {
mPrefs = getActivity().getSharedPreferences(MAP_PREFS, Context.MODE_PRIVATE);
mBaseMap = mPrefs.getInt(PREF_BASE_MAP, BASE_MAP_OTM);
mOverlay = mPrefs.getInt(PREF_OVERLAY, OverlayHelper.OVERLAY_NONE);
mPrefs = requireActivity().getSharedPreferences(MAP_PREFS, Context.MODE_PRIVATE);
mBaseMap = mPrefs.getInt(PREF_BASE_MAP, BASE_MAP_OTM);
mOverlay = mPrefs.getInt(PREF_OVERLAY, OverlayHelper.OVERLAY_NONE);
mLocationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE);
}


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mMapCenterState = new GeoPoint(savedInstanceState.getDouble(STATE_LATITUDE, 0),
savedInstanceState.getDouble(STATE_LONGITUDE, 0));
}
}

Expand Down Expand Up @@ -190,14 +208,18 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
mMapView.getOverlays().add(this.mCompassOverlay);
mMapView.getOverlays().add(this.mScaleBarOverlay);

if (mMapCenterState != null) {
mMapView.getController().setCenter(mMapCenterState);
}

mMapView.addMapListener(new DelayedMapListener(mDragListener));

mCopyRightView = view.findViewById(R.id.copyrightView);

setBaseMap();

mLocationOverlay.enableMyLocation();
mLocationOverlay.enableFollowLocation();
mLocationOverlay.disableFollowLocation();
mLocationOverlay.setOptionsMenuEnabled(true);
mCompassOverlay.enableCompass();
mMapView.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -226,6 +248,19 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
if (mListener.getSelectedNearbyPlace() != null) {
showNearbyPlace(mListener.getSelectedNearbyPlace());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (requireActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
requireActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mCurrentLocation = mLocationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
} else {
mCurrentLocation = mLocationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
if (mMapCenterState != null) {
mMapView.getController().setCenter(mMapCenterState);
} else if (mCurrentLocation != null) {
mMapView.getController().setCenter(new GeoPoint(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()));
}

}

Expand Down Expand Up @@ -315,6 +350,28 @@ private void disableFollow() {
mMapHandler.removeCallbacks(mCenterRunnable);
}

@SuppressLint("MissingPermission")
@Override
public void onResume() {
super.onResume();
//File basePath = Configuration.getInstance().getOsmdroidBasePath();
//File cache = Configuration.getInstance().getOsmdroidTileCache();
initMap();
if (mLocationManager != null) {
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0f, this);
} catch (Exception ex) {
ex.printStackTrace();

}
try {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, this);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

@Override
public void onPause() {
super.onPause();
Expand All @@ -332,28 +389,13 @@ public void onPause() {
mScaleBarOverlay.disableScaleBar();
}

@SuppressLint("MissingPermission")
@Override
public void onResume() {
super.onResume();
//File basePath = Configuration.getInstance().getOsmdroidBasePath();
//File cache = Configuration.getInstance().getOsmdroidTileCache();
initMap();
if (getActivity() != null) {
mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager != null) {
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0f, this);
} catch (Exception ex) {
ex.printStackTrace();

}
try {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, this);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
if (mMapView != null) {
mMapCenterState = (GeoPoint) mMapView.getMapCenter();
outState.putDouble(STATE_LATITUDE, mMapCenterState.getLatitude());
outState.putDouble(STATE_LONGITUDE, mMapCenterState.getLongitudeE6());
}
}

Expand Down Expand Up @@ -652,5 +694,6 @@ public interface OnFragmentInteractionListener {
*/
NearbyItem getSelectedNearbyPlace();
}

}

0 comments on commit 1685aaf

Please sign in to comment.