Based on the location the user will be Discovere for the nearest Caffe through use the retrofit library to specify the request parameters and embed the place type and Latitude, Longitude within the request. and send it via Create a custom HTTP header. After that Send the GET HTTP request to the Google Place API server included with the API key to authenticate the request plus the customize HTTP header to get the server response via get nearby Caffe.
Steps 5:
- Setting up dependencies
- Setting up permissions
- Setting up view
- Setting up networking with Retrofit
- Cross check.
Android Manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
More on AndroidManifest
Map
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
More on Activity view
Api service provider
public NearByApi getApiService() {
if (nearByApi == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().retryOnConnectionFailure(true).readTimeout(80, TimeUnit.SECONDS).connectTimeout(80, TimeUnit.SECONDS).addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constant.PLACE_API_BASE_URL).addConverterFactory(getApiConvertorFactory()).client(client).build();
nearByApi = retrofit.create(NearByApi.class);
return nearByApi;
} else {
return nearByApi;
}
}
private static GsonConverterFactory getApiConvertorFactory() {
return GsonConverterFactory.create();
}
public static MyApplication getApp() {
return app;
}
More on Application Class
Api call for finding places:
public void findPlaces(String placeType){
Call<NearByApiResponse> call = MyApplication.getApp().getApiService().getNearbyPlaces(placeType, location.getLatitude() + "," + location.getLongitude(), PROXIMITY_RADIUS);
call.enqueue(new Callback<NearByApiResponse>() {
@Override
public void onResponse(Call<NearByApiResponse> call, Response<NearByApiResponse> response) {
try {
googleMap.clear();
// This loop will go through all the results and add marker on each location.
for (int i = 0; i < response.body().getResults().size(); i++) {
Double lat = response.body().getResults().get(i).getGeometry().getLocation().getLat();
Double lng = response.body().getResults().get(i).getGeometry().getLocation().getLng();
String placeName = response.body().getResults().get(i).getName();
String vicinity = response.body().getResults().get(i).getVicinity();
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat, lng);
// Position of Marker on Map
markerOptions.position(latLng);
// Adding Title to the Marker
markerOptions.title(placeName + " : " + vicinity);
// Adding Marker to the Camera.
Marker m = googleMap.addMarker(markerOptions);
// Adding colour to the marker
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
// move map camera
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(11));
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Call<NearByApiResponse> call, Throwable t) {
Log.d("onFailure", t.toString());
t.printStackTrace();
PROXIMITY_RADIUS += 10000;
}
});
}