Android Google Maps Simple Usage — Google Places API
Google Maps Platform
Google Maps Platform provides three main explore products as below.
1. Maps
2. Routes
3. Places
In this part, I am going to find out the bars nearby the screen center via Google Place API.
The basic API request set up is from these articles I wrote before.
Enable Places API
We have to enable some setting in GCP to fetch the data from Places API.
First, find the Places API for Web and enable it as below.
After enable this, we could use the key we have generated to fetch Places API.
Places API Documents
Basically, Google almost list every set up flow for us. For the quick implement. We find the nearby search request in the document for the demand I mention above “Find out the bars nearby the screen center”.
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=lat,lng&radius=1000&type=bar&key=YOUR_API_KEY
Obviously, nearby search is a GET request. So we could use browser or Postman to help us fetch the JSON data.
Retrofit setting
As usual, build up Retrofit for request Places API.
Reference this to read the detail about Retrofit.
RetrofitClient
Same as example we created, just change the name and base url.
GoogleMethods
There are many parameters been mention in the documents. But I only pick required parameters(location, radius, key) and an optional parameter(type).
Model
It is easy to create Nearby Search model by this plugin.
Moreover, we also could create a model called Spot to manage the custom spot.(Optional)
Constants
Also, don’t forget to create a constants file to manage constants.
Here are the files we created so far.
There is an empty folder named directions is for the next part.
Separate those file as possible as we could, prevent mix them into one folder. So that we could find or fix our code much easier.
Request Places API
In this project, I choose to use toggle button to manage those bar markers show or not.
Here is how to parse places data and put them into a custom Spot list.
MapController
In this section, I create a global list named mSpotMarkerList to store the status of the markers that we put on the map so that could clear those markers.
private var mSpotMarkerList = ArrayList<Marker>()
setMarkersAndZoom
After we fetch the Places data and generate the Spot list, using this function to draw a custom marker and put them on the map.
clearMarker
This function helps to clear the markers we have generated.
autoZoomLevel(Optional)
The reason why I wrote this function is to implement a tricky demand.
If we put a list of spot into this function, it will return a CameraUpdate for moving the camera to contain all markers.
These are how we fetch Places API and put those markers on the map.
Furthermore, Directions API is another function we always use in our daily life. Uber, for example, users always see the route between car and them when they are waiting for the driver.
So, fetching Directions API and draw the route on the map will be the next goal.
- Android Google Maps Simple Usage — Introduction
- Android Google Maps Simple Usage — Initial Project with Map
- Android Google Maps Simple Usage — Custom Marker and Zoom Function
- Android Google Maps Simple Usage — Google Places API (you’re here)
- Android Google Maps Simple Usage — Google Directions API
- Android Google Maps Simple Usage — Conclusion