|
@@ -6,63 +6,51 @@ import android.content.pm.PackageManager;
|
|
|
import android.os.Bundle;
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
-import androidx.annotation.Nullable;
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
-import androidx.fragment.app.Fragment;
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
import androidx.fragment.app.FragmentTransaction;
|
|
|
-
|
|
|
-
|
|
|
-import android.util.Log;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
import android.widget.ImageButton;
|
|
|
import android.widget.LinearLayout;
|
|
|
-import android.widget.Toast;
|
|
|
-
|
|
|
-
|
|
|
import com.google.android.gms.location.FusedLocationProviderClient;
|
|
|
import com.google.android.gms.location.LocationServices;
|
|
|
-
|
|
|
+import com.sharix.sportsmanfriend.BaseFragmentClass;
|
|
|
import com.sharix.sportsmanfriend.R;
|
|
|
-
|
|
|
import org.osmdroid.api.IMapController;
|
|
|
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
|
|
|
import org.osmdroid.util.GeoPoint;
|
|
|
import org.osmdroid.views.MapView;
|
|
|
|
|
|
-public class MapFragment extends Fragment {
|
|
|
-
|
|
|
+public class MapFragment extends BaseFragmentClass {
|
|
|
+ // константы
|
|
|
private final int REQUEST_PERMISSIONS_REQUEST_CODE = 1;
|
|
|
private final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 2;
|
|
|
+ private final String KEY_ZOOM_LEVEL = "zoomLevel";
|
|
|
+ private final String KEY_CENTER_LATITUDE = "centerLatitude";
|
|
|
+ private final String KEY_CENTER_LONGITUDE = "centerLongitude";
|
|
|
+ //Элементы
|
|
|
private MapView map = null;
|
|
|
- private IMapController mapController = null;
|
|
|
- private View view = null;
|
|
|
- private ImageButton btnZoomIn;
|
|
|
- private ImageButton btnZoomOut;
|
|
|
- private ImageButton btnMyLocation;
|
|
|
+ private ImageButton btnZoomIn, btnZoomOut, btnMyLocation;
|
|
|
private LinearLayout btnOpenEventList;
|
|
|
- private double latitude, longitude;
|
|
|
+ //определяет последнюю известную локацию
|
|
|
private FusedLocationProviderClient fusedLocationProviderClient;
|
|
|
+ //контроллер карты OSM
|
|
|
+ IMapController mapController;
|
|
|
|
|
|
- private Bundle savedState = null;
|
|
|
-
|
|
|
- private static final String KEY_ZOOM_LEVEL = "zoomLevel";
|
|
|
- private static final String KEY_CENTER_LATITUDE = "centerLatitude";
|
|
|
- private static final String KEY_CENTER_LONGITUDE = "centerLongitude";
|
|
|
+ private double latitude, longitude;
|
|
|
|
|
|
- private double centerLatitude = 0.0;
|
|
|
- private double centerLongitude = 0.0;
|
|
|
- private int zoomLevel = 0;
|
|
|
+ private static final int REQUEST_CODE = 1;
|
|
|
+ private String[] permissions = {Manifest.permission.READ_CONTACTS};
|
|
|
|
|
|
@Override
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
|
|
- view = inflater.inflate(R.layout.fragment_map, container, false);
|
|
|
-
|
|
|
+ View view = inflater.inflate(R.layout.fragment_map, container, false);
|
|
|
|
|
|
+ //
|
|
|
btnZoomIn = view.findViewById(R.id.btn_zoomIn);
|
|
|
btnZoomOut = view.findViewById(R.id.btn_zoomOut);
|
|
|
btnMyLocation = view.findViewById(R.id.btnMyLocation);
|
|
@@ -71,24 +59,12 @@ public class MapFragment extends Fragment {
|
|
|
map = view.findViewById(R.id.map);
|
|
|
mapController = map.getController();
|
|
|
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
|
|
|
- if(savedInstanceState != null && savedState == null) {
|
|
|
- savedState = savedInstanceState.getBundle("key1");
|
|
|
- Log.d("TAG", "first ");
|
|
|
- }
|
|
|
- if(savedState != null) {
|
|
|
- zoomLevel = savedState.getInt(KEY_ZOOM_LEVEL);
|
|
|
- centerLatitude = savedState.getDouble(KEY_CENTER_LATITUDE);
|
|
|
- centerLongitude = savedState.getDouble(KEY_CENTER_LONGITUDE);
|
|
|
- configureMap(centerLatitude, centerLongitude, zoomLevel);
|
|
|
- Log.d("TAG", "second");
|
|
|
- }
|
|
|
- else if(savedState == null){
|
|
|
+
|
|
|
+ if (!hasSavedState()){
|
|
|
configureMap(55.934473, 37.497081, 15);
|
|
|
- Log.d("TAG", "thirt");
|
|
|
}
|
|
|
|
|
|
- savedState = null;
|
|
|
-
|
|
|
+ ActivityCompat.requestPermissions(getActivity(), permissions, REQUEST_CODE);
|
|
|
askPermissions();
|
|
|
addClickEventToAllButtons();
|
|
|
|
|
@@ -126,8 +102,8 @@ public class MapFragment extends Fragment {
|
|
|
|
|
|
private void askPermissions() {
|
|
|
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED ||
|
|
|
- ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
|
|
|
- ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
|
|
|
+ ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
String[] permission = {Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION};
|
|
|
requestPermissions(permission, REQUEST_PERMISSIONS_REQUEST_CODE);
|
|
|
}
|
|
@@ -149,7 +125,7 @@ public class MapFragment extends Fragment {
|
|
|
|
|
|
btnMyLocation.setOnClickListener(v -> {
|
|
|
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
|
|
- && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
String[] permission = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
|
|
|
requestPermissions(permission, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
|
|
|
}
|
|
@@ -188,25 +164,29 @@ public class MapFragment extends Fragment {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void onDestroyView() {
|
|
|
- super.onDestroyView();
|
|
|
- savedState = saveState();
|
|
|
+ protected boolean hasSavedState() {
|
|
|
+ Bundle state = getSavedState();
|
|
|
+
|
|
|
+ if (state == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ int zoomLevel = state.getInt(KEY_ZOOM_LEVEL);
|
|
|
+ double centerLatitude = state.getDouble(KEY_CENTER_LATITUDE);
|
|
|
+ double centerLongitude = state.getDouble(KEY_CENTER_LONGITUDE);
|
|
|
+ configureMap(centerLatitude, centerLongitude, zoomLevel);
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- private Bundle saveState() { /* called either from onDestroyView() or onSaveInstanceState() */
|
|
|
+ @Override
|
|
|
+ protected Bundle getStateToSave() {
|
|
|
Bundle state = new Bundle();
|
|
|
+
|
|
|
state.putInt(KEY_ZOOM_LEVEL, map.getZoomLevel());
|
|
|
state.putDouble(KEY_CENTER_LATITUDE, map.getMapCenter().getLatitude());
|
|
|
state.putDouble(KEY_CENTER_LONGITUDE, map.getMapCenter().getLongitude());
|
|
|
- return state;
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public void onSaveInstanceState(Bundle outState) {
|
|
|
- super.onSaveInstanceState(outState);
|
|
|
- /* If onDestroyView() is called first, we can use the previously savedState but we can't call saveState() anymore */
|
|
|
- /* If onSaveInstanceState() is called first, we don't have savedState, so we need to call saveState() */
|
|
|
- /* => (?:) operator inevitable! */
|
|
|
- outState.putBundle("key1", (savedState != null) ? savedState : saveState());
|
|
|
+ return state;
|
|
|
}
|
|
|
}
|