0

The official documentation of Places API client library (for GetPlace) has a sample usage of the client library as below

import com.google.api.core.ApiFuture;
import com.google.maps.places.v1.GetPlaceRequest;
import com.google.maps.places.v1.Place;
import com.google.maps.places.v1.PlaceName;
import com.google.maps.places.v1.PlacesClient;

public class AsyncGetPlace {

  public static void main(String[] args) throws Exception {
    asyncGetPlace();
  }

  public static void asyncGetPlace() throws Exception {
    // This snippet has been automatically generated and should be regarded as a code template only.
    // It will require modifications to work:
    // - It may require correct/in-range values for request initialization.
    // - It may require specifying regional endpoints when creating the service client as shown in
    // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
    try (PlacesClient placesClient = PlacesClient.create()) {
      GetPlaceRequest request =
          GetPlaceRequest.newBuilder()
              .setName(PlaceName.of("[PLACE_ID]").toString())
              .setLanguageCode("languageCode-2092349083")
              .setRegionCode("regionCode-1991004415")
              .setSessionToken("sessionToken-696552189")
              .build();
      ApiFuture<Place> future = placesClient.getPlaceCallable().futureCall(request);
      // Do something.
      Place response = future.get();
    }
  }
}

Where can I specify the fields to return?

What are .setLanguageCode, .setRegionCode, .setSessionToken?

I'm assuming .setSessionToken is for autocomplete function, but I'm not sure what the first two are. I haven't seen them in their HTTP GET request usages.

I've been searching through their documentation on Github for a couple of days, accidentally implemented the old Places API, and I am fixing my codes to use the new APIs. However, their library structure is quite broad and it was difficult to find what I want to find.

I also couldn't access their Client Library Documentation page, so if anyone was able to visit the page, please let me know.

1 Answer 1

0

You will also need to use FieldMask to specify the fields to return in the Places API client library for Java. The FieldMask class can be imported from com.google.protobuf, and for more information about how to construct field masks, see the field_mask.proto which is also referenced in Places API documentation.

After importing the FieldMask class,

import com.google.protobuf.FieldMask

Add it to your GetPlaceRequest. You can use the setFieldMask method to specify the fields you want to return. Like so:

GetPlaceRequest request =
          GetPlaceRequest.newBuilder()
              .setName(PlaceName.of("[PLACE_ID]").toString())
              .setLanguageCode("languageCode-2092349083")
              .setRegionCode("regionCode-1991004415")
              .setSessionToken("sessionToken-696552189")
              .setFieldMask(FieldMask.newBuilder().addPaths("formattedAddress").build())
              .build();

As for your other question regarding setLanguageCode, .setRegionCode, and .setSessionToken, the latter indeed is to set the session token which are used in an autocomplete session. For the other two,

  • setLanguageCode - This is to specify the language in which the result should be returned. For example, "en" for English.
  • setRegionCode - This sets the region code, which influences the results to favor the specified region. For example, "US" for United States.

Worth noting: The values inside the parentheses for each method like "languageCode-2092349083" are arbitrary placeholder values as the sample code snippet has been automatically generated, and are meant to replace with actual values.

1
  • Hi Kat, thank you for your response. I have already implemented my app with plain HTTP requests, but I will update them with client libraries in the future. By the way, where did you find the documentation for setting the FieldMasks?
    – hnbm
    Commented Jul 1 at 12:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.