Skip to content
Snippets Groups Projects
Commit 9e2d7a51 authored by COUCHET Thibaud's avatar COUCHET Thibaud
Browse files

App with maps work without intraction plus riches

parent 8115bdb4
No related branches found
No related tags found
No related merge requests found
...@@ -21,12 +21,17 @@ import com.google.android.gms.maps.CameraUpdateFactory; ...@@ -21,12 +21,17 @@ import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
public class MapsFragment extends Fragment { public class MapsFragment extends Fragment {
private Toolbar toolbar; private Toolbar toolbar;
private GoogleMap mMap;
private final ArrayList<LatLng> markerPositions = new ArrayList<>();
public MapsFragment() { public MapsFragment() {
// Required empty public constructor // Required empty public constructor
...@@ -49,6 +54,12 @@ public class MapsFragment extends Fragment { ...@@ -49,6 +54,12 @@ public class MapsFragment extends Fragment {
*/ */
@Override @Override
public void onMapReady(GoogleMap googleMap) { public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Redessine les anciens marqueurs si existants
for (LatLng position : markerPositions) {
googleMap.addMarker(new MarkerOptions().position(position)
.title("Lat: " + position.latitude + ", Lng: " + position.longitude));
}
// We check for the application "Call permission" // We check for the application "Call permission"
if (ContextCompat.checkSelfPermission(requireContext(), if (ContextCompat.checkSelfPermission(requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
...@@ -60,6 +71,37 @@ public class MapsFragment extends Fragment { ...@@ -60,6 +71,37 @@ public class MapsFragment extends Fragment {
googleMap.getUiSettings().setZoomGesturesEnabled(true); googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true); googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true); googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
double latitude = latLng.latitude;
double longitude = latLng.longitude;
String message = "Latitude : " + latitude + "\nLongitude : " + longitude;
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
double latitude = latLng.latitude;
double longitude = latLng.longitude;
String title = "Lat: " + latitude + ", Lng: " + longitude;
// Crée le marker
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title(title);
// Ajoute le marker à la carte
googleMap.addMarker(markerOptions);
markerPositions.add(latLng);
// Affiche un toast
Toast.makeText(requireContext(), "Marqueur ajouté à " + title, Toast.LENGTH_SHORT).show();
}
});
} catch (SecurityException e) { } catch (SecurityException e) {
Log.e("MapsFragment", "Error while enabling location", e); Log.e("MapsFragment", "Error while enabling location", e);
} }
...@@ -82,6 +124,12 @@ public class MapsFragment extends Fragment { ...@@ -82,6 +124,12 @@ public class MapsFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) { @Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
ArrayList<LatLng> savedMarkers = savedInstanceState.getParcelableArrayList("markers");
if (savedMarkers != null) {
markerPositions.addAll(savedMarkers);
}
}
return inflater.inflate(R.layout.fragment_maps, container, false); return inflater.inflate(R.layout.fragment_maps, container, false);
} }
...@@ -94,13 +142,13 @@ public class MapsFragment extends Fragment { ...@@ -94,13 +142,13 @@ public class MapsFragment extends Fragment {
toolbar.setOnMenuItemClickListener(item -> { toolbar.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.MapTypeNormalAction) { if (item.getItemId() == R.id.MapTypeNormalAction) {
shareAction(); mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return true; return true;
} else if (item.getItemId() == R.id.MapTypeHybridAction) { } else if (item.getItemId() == R.id.MapTypeHybridAction) {
openWikipedia(); mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
return true; return true;
} else if (item.getItemId() == R.id.MapTypeSatelliteAction) { } else if (item.getItemId() == R.id.MapTypeSatelliteAction) {
resetAllFields(); mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
return true; return true;
} }
return false; return false;
...@@ -112,4 +160,10 @@ public class MapsFragment extends Fragment { ...@@ -112,4 +160,10 @@ public class MapsFragment extends Fragment {
mapFragment.getMapAsync(callback); mapFragment.getMapAsync(callback);
} }
} }
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("markers", markerPositions);
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment