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
//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"
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; }
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>() { @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!
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.
You may also like,
- Android Tutorial: what you need to know about retrofit and how to integrate it for Network calling?
- How to reduce Android app size?
This page was last edited on December 28th, 2020, at 8:10.