Skip to content

Instantly share code, notes, and snippets.

@walidum
Created August 17, 2021 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walidum/855ce2d44c581de5131a8e362b1e8a13 to your computer and use it in GitHub Desktop.
Save walidum/855ce2d44c581de5131a8e362b1e8a13 to your computer and use it in GitHub Desktop.
This method demonstrates the programmatic approach to getting place predictions.
*
* This method demonstrates the programmatic approach to getting place predictions.
* The parameters in this request are currently biased to Kolkata, India.
*
* @param query the plus code query string (e.g. "GCG2+3M K")
*/
private void getPlacePredictions(String query) {
// The value of `locationBias` biases prediction results to the rectangular
// region provided (currently Kolkata). Modify these values to get results
// to another area. Make sure to pass in the appropriate value/s for
// .setCountries() as in the FindAutocompletePredictionsRequest.Builder
// object as well.
LocationBias locationBias = RectangularBounds.newInstance(
new LatLng(22.458744, 88.208162),
new LatLng(22.730671, 88.524896));
// Create a new programmatic Place Autocomplete request in Places SDK for Android
FindAutocompletePredictionsRequest newRequest = FindAutocompletePredictionsRequest.builder()
.setLocationBias(locationBias)
.setQuery(query)
.setTypeFilter(TypeFilter.ESTABLISHMENT)
.setCountries("IN")
.build();
// Perform autocomplete predictions request
placesClient.findAutocompletePredictions(newRequest).addOnSuccessListener((response) -> {
List<AutocompletePrediction> predictions = response.getAutocompletePredictions();
if (predictions.isEmpty()) {
Log.w(TAG, "No predictions found");
return;
}
// Perform Geocoding API request from 1st prediction match
AutocompletePrediction firstPrediction = predictions.get(0);
geocodePlace(firstPrediction.getPlaceId());
}).addOnFailureListener((exception) -> {
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + apiException.getStatusCode());
}
});
}
// The Volley dispatch queue for network requests. Initialized with an Android Context object
RequestQueue queue;
/**
* Performs a Geocode API request
*
* @param placeID the ID of the Place to geocode
*/
private void geocodePlace(String placeID) {
// Construct the request URL
String apiKey = ""; // Your GMP API key
String url = "https://maps.googleapis.com/maps/api/geocode/json?place_id=%s&key=%s";
String requestURL = String.format(url, placeID, apiKey);
// Use the HTTP request URL for Geocoding API to get geographic coordinates for the place
JsonObjectRequest request = new JsonObjectRequest(Method.GET, requestURL, null,
response -> {
try {
// Inspect the value of "results" and make sure it's not empty
JSONArray results = response.getJSONArray("results");
if (results.length() == 0) {
Log.w(TAG, "No results from geocoding request.");
return;
}
// Use Gson to convert the response JSON object to a POJO
GeocodingResult result = new Gson()
.fromJson(results.getString(0), GeocodingResult.class);
LatLng latLng = result.geometry.location;
Log.d(TAG, "LatLng for geocoded place: " + latLng);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> Log.e(TAG, "Request failed"));
// Add the request to the Request queue.
queue.add(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment