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

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.


Now, select Empty Activity and click next.


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


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

Setting up dependencies

Build.gradle dependencies

compile ''
compile ''

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


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

Setting up networking with Retrofit

public NearByApi getApiService() {
if (nearByApi == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
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;

Want To Develop An Android Application?

Validate your app idea and get a free quote.

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>() {
public void onResponse(Call<NearByApiResponse> call, Response<NearByApiResponse> response) {
try {
// 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
// Adding Title to the Marker
markerOptions.title(placeName + " : " + vicinity);
// Adding Marker to the Camera.
Marker m = googleMap.addMarker(markerOptions);
// Adding colour to the marker
// move map camera
} catch (Exception e) {
Log.d("onResponse", "There is an error");

public void onFailure(Call<NearByApiResponse> call, Throwable t) {
Log.d("onFailure", t.toString());

And done!


Now, in this android tutorial, you’ve learned how to integrate Google nearby notifications 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.

Author Bio
Amit Patolia
Amit Patolia
Designation: Android Team Lead

Amit Patolia is an Android Team Lead at Space-O Technologies. He has over 9+ years of experience in Android app development. He has guided to develop over 100 Android apps with unique features and functionalities. He also has expertise in Kotlin-based apps.