How to Integrate Google Nearby API in Android App Using Retrofit Android

0
Shares
How to Integrate Google Nearby API in Android App Using Retrofit Android
4 (80%) 1 vote

google nearby api

Google nearby API is one of the many APIs provided by google to get geographical information about nearby places like restaurants, gas stations, cafes, bars, hospitals using HTTP request.

For this, we’ll need to pass the latitude and longitude coordinates of the location to know about the nearby places’ location. To get these data, we’ll need to integrate Google nearby API.

In this Android app tutorial, we’ll first get current location and then use Google nearby API to get location of nearby places like restaurants and hospitals.

Let’s Get Started

First, open Android Studio and create a new project.

In the next step, select targeted Android device.

project_min_sdk_level

Now, select Empty Activity and click next.

project_activity_selection

Lastly, write your activity name and details and click on finish.

project_activity_name

Once you’ve created a new project, start code integration.

Setting up dependencies

Build.gradle dependencies

//map
compile 'com.google.android.gms:play-services-maps:10.2.1'
//location
compile 'com.google.android.gms:play-services-location:10.2.0'

//Retrofit networking api
compile "com.squareup.retrofit2:retrofit:2.1.0"
compile "com.squareup.retrofit2:converter-gson:2.1.0"
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile "com.squareup.okhttp3:logging-interceptor:3.4.1"

build_gradle

Setting up permissions

<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"/>

Setting up view

<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"/>

Setting up networking with Retrofit

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;
}

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;
}
});
}

And done!

restaurants

Now, in this android tutorial, you’ve learned how to integrate Google nearby API to display nearby restaurants and hospitals. Though, if you want to add further features, the implementation process can get tricky, and it’s better to consult with an Android app development company before you implement in your native Android app.

Grab a free copy of Google Nearby demo from Github.

 
0
Shares
 

Want to Develop Android App From Scratch? Contact Us Now