1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| public class MapFragment extends Fragment implements OnMapReadyCallback {
private Location mLocation;
private FusedLocationProviderClient mFusedLocationProviderClient;
private GoogleMap mMap;
private final RetrofitRepository mRetrofitRepository = new RetrofitRepository(APIClient.getGoogleMapAPI());
int radius = 1000;
private static final float DEFAULT_ZOOM = 15;
private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085);
public static MapFragment newInstance() {
return (new MapFragment());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_map, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
assert mapFragment != null;
mapFragment.getMapAsync(this);
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(Objects.requireNonNull(getActivity()));
FloatingActionButton floatingActionButton = view.findViewById(R.id.fab_location);
floatingActionButton.setOnClickListener(v -> getCurrentLocation());
}
private void getCurrentLocation() {
Dexter.withContext(getActivity())
.withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new PermissionListener() {
@SuppressLint("MissingPermission")
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
mFusedLocationProviderClient.getLastLocation().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
mLocation = task.getResult();
if (mLocation != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLocation.getLatitude(), mLocation.getLongitude()), DEFAULT_ZOOM));
} else {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
mMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
});
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();
}
@SuppressLint("MissingPermission")
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
mMap = googleMap;
getCurrentLocation();
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mFusedLocationProviderClient.getLastLocation().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
mLocation = task.getResult();
mRetrofitRepository.getPlaceResultsLiveData(mLocation.toString()).observe(requireActivity(), restaurants -> {
try {
mMap.clear();
for (Result r : restaurants.getResult()) {
double lat = r.getGeometry().getLocation().getLat();
double lng = r.getGeometry().getLocation().getLng();
String name = r.getName();
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(name);
mMap.addMarker(markerOptions);
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
});
}
});
}
} |
Partager