Salut Migwel,
Voici ce que j’ai fais.
J’ai un peu avancé mais je bloque toujours.
J’ai donc mon interface GoogleMapAPI qui va me servir a faire les requêtes :
public interface GoogleMapAPI {
@GET("nearbysearch/json")
Call<PlaceResults> getNearby(@Query("location") String location,
@Query("radius") int radius,
@Query("type") String type,
@Query("key") String key);
@GET("details/json")
Call<DetailsPlaces> getDetailsPlaces(@Query("place_id") String placeId,
@Query("fields") String fields,
@Query("key") String key);
}
Ensuite j’ai ma classe PlaceResults qui va me servir à récupérer les infos sur les éléments que j’ai récupéré :
public class PlaceResults {
@SerializedName("results")
@Expose
private final List<Result> results;
public PlaceResults(List<Result> results) {
this.results = results;
}
public List<Result> getResult() {
return results;
}
}
Puis dans ma classe RetrofitRepository qui va me servir à faire mon call j’ai :
public class RetrofitRepository {
private final GoogleMapAPI mGoogleMapAPI;
public RetrofitRepository(GoogleMapAPI googleMapAPI) {
this.mGoogleMapAPI = googleMapAPI;
}
public LiveData <PlaceResults> getPlaceResultsLiveData(String location, int radius, String type, String apiKey) {
MutableLiveData <PlaceResults> PlaceResultsMutableLiveData = new MutableLiveData<>();
Call<PlaceResults> placeResultsCall = mGoogleMapAPI.getNearby(location, radius, type, apiKey);
placeResultsCall.enqueue(new Callback<PlaceResults>() {
@Override
public void onResponse(@NonNull Call<PlaceResults> call, @NonNull Response<PlaceResults> response) {
PlaceResultsMutableLiveData.setValue(response.body());
}
@Override
public void onFailure(@NonNull Call<PlaceResults> call, @NonNull Throwable t) {
PlaceResultsMutableLiveData.setValue(null);
}
});
return PlaceResultsMutableLiveData;
}
}
Et pour finir mon MapFragment qui me permet d’afficher la map, récupérer ma geolocalisation et dans lequel j’aimerais observer mes éléments récupérer pour afficher uniquement des restaurants sur la map quand celle ci s’affiche :
public class MapFragment extends Fragment implements OnMapReadyCallback {
private FloatingActionButton mFloatingActionButton;
private Location mLocation;
private FusedLocationProviderClient mFusedLocationProviderClient;
private GoogleMap mMap;
RetrofitRepository mRetrofitRepository;
int radius = 1500;
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);
Places.initialize(Objects.requireNonNull(getActivity()), BuildConfig.MAPS_API_KEY);
mPlacesClient = Places.createClient(getActivity());
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(Objects.requireNonNull(getActivity()));
mFloatingActionButton = view.findViewById(R.id.fab_location);
mFloatingActionButton.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));
mRetrofitRepository.getPlaceResultsLiveData(new LatLng(mLocation.getLatitude(), mLocation.getLongitude()).toString(), radius, "restaurant", BuildConfig.MAPS_API_KEY).observe(requireActivity(), restaurants -> {
mMap.clear();
for (Result r : restaurants.getResult() ){
restaurants.getResult().get(r).toString().getLocation().getLatitude();
}
});
} 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();
}
}
J’essaie de faire une boucle for pour récuprer mes éléments mais ça me dit "Required type:int Provided:Result" sur le get®.
Merci d’avance pour ton aide