package fr.imt_atlantique.myfirstapplication; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import android.Manifest; import android.app.AlertDialog; import android.content.pm.PackageManager; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MapsFragment extends Fragment { private Toolbar toolbar; public MapsFragment() { // Required empty public constructor } public static MapsFragment newInstance() { return new MapsFragment(); } private OnMapReadyCallback callback = new OnMapReadyCallback() { /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. * In this case, we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to * install it inside the SupportMapFragment. This method will only be triggered once the * user has installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { // We check for the application "Call permission" if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { grantPermissionAlertDialog(); return; } try { googleMap.setMyLocationEnabled(true); googleMap.getUiSettings().setZoomGesturesEnabled(true); googleMap.getUiSettings().setCompassEnabled(true); googleMap.getUiSettings().setMyLocationButtonEnabled(true); } catch (SecurityException e) { Log.e("MapsFragment", "Error while enabling location", e); } } }; private void grantPermissionAlertDialog() { new AlertDialog.Builder(requireContext()) .setTitle(getString(R.string.position_permission_title)) .setMessage(getString(R.string.position_permission_message)) .setNegativeButton(getString(R.string.cancel), (dialog, which) -> Toast.makeText(requireContext(), getString(R.string.permission_denied), Toast.LENGTH_SHORT) .show()) .setPositiveButton("Ok", (dialog, which) -> ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 3)) .create() .show(); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_maps, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); toolbar = view.findViewById(R.id.mapsToolbar); toolbar.inflateMenu(R.menu.menu_maps); SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); if (mapFragment != null) { mapFragment.getMapAsync(callback); } } }