I am referring to Nearby Search (New).
Every request requires a Field Mask set outside of the request proto.
import {Client} from @googlemaps/google-maps-services-js
, but it always calls the legacy Nearby Search.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);
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