最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to include FieldMask in Nearby Search (New)? - Stack Overflow

programmeradmin3浏览0评论

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);

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);

Share Improve this question asked Mar 17 at 14:54 NirNir 1132 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The documentation is a bit lacking but I found the correct info here: https://www.npmjs/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/maps/documentation/javascript/nearby-search#javascript

发布评论

评论列表(0)

  1. 暂无评论