node.jstypescriptfirebasegoogle-mapsgoogle-places-api

How to include FieldMask in Nearby Search (New)?


I am referring to Nearby Search (New).

Every request requires a Field Mask set outside of the request proto.

  1. Could you explain how do I set this header with searchNearby using v1.PlacesClient?
  2. Is there a way to use @googlemaps/google-maps-services-js instead of v1.PlacesClient? I've tried this library using import {Client} from @googlemaps/google-maps-services-js, but it always calls the legacy Nearby Search.
  3. Does it make sense to use v1.PlacesClient with backend/server-side applications or only with frontend applications?

Here is my code snippet:

// Import the Places library
import places from '@googlemaps/places';
    
// Instantiate a client
const placesClient = new places.v1.PlacesClient(
  {
    apiKey: process.env.GOOGLE_MAPS_API_KEY!
  }
);
    
const requestParams = {
          locationRestriction: {
            circle: {
              center: {
                latitude: 48.1351253,
                longitude: 11.5819806,
              },
            },
            radius: 3000,
          },
          includedTypes: ['restaurant'], // restrict the results to places matching the specified type.
          language: 'en', // specify the language of the response.
          maxResultCount: 20, // maximum number of results to return.
        };
    
const requestOptions = { timeout: 1000 }
    
const placesNearbyResponse = await placesClient.searchNearby(requestParams, requestOptions);

enter image description here


Solution

  • The documentation is a bit lacking but I found the correct info here: https://www.npmjs.com/package/@googlemaps/places

    const placesNearbyResponse = await placesClient.searchNearby(requestParams, {
      otherArgs: {
        headers: {
          "X-Goog-FieldMask": "places.displayName",
        },
      },
    });
    

    I would not recommend to use @googlemaps/google-maps-services-js since it uses the legacy API's for now.

    v1.PlacesClient is meant more for backend, the way you configured it I think it would use gRPC. For the Frontend the REST API is the more regular choice. https://developers.google.com/maps/documentation/javascript/nearby-search#javascript