Places Aggregate API client library examples

This page has examples of how to use the Places Aggregate API client libraries.

Install the client libraries

SeePlaces Aggregate API clientlibrariesfor installation instructions.

Authentication

When you use client libraries, you useApplication Default Credentials(ADC) to authenticate. For information about setting up ADC, seeProvide credentialsfor Application DefaultCredentials. For information about using ADC withclient libraries, seeAuthenticate using clientlibraries .

You can also use API keys to authenticate to the client libraries, for example:

Java

importjava.util.HashMap;importjava.util.Map;importcom.google.api.gax.core.NoCredentialsProvider;importcom.google.api.gax.rpc.FixedHeaderProvider;importcom.google.api.gax.rpc.HeaderProvider;importcom.google.maps.areainsights.v1.AreaInsightsClient;importcom.google.maps.areainsights.v1.AreaInsightsSettings;// ...StringapiKey="API_KEY";// Create a HeaderProvider to inject the API key into every request.Map<String,String>headers=newHashMap<>();headers.put("x-goog-api-key",apiKey);HeaderProviderheaderProvider=FixedHeaderProvider.create(headers);// Configure the client settings.AreaInsightsSettingsareaInsightsSettings=AreaInsightsSettings.newBuilder()// Use the header provider to add the API key..setHeaderProvider(headerProvider)// Tell the client not to use Application Default Credentials..setCredentialsProvider(NoCredentialsProvider.create()).build();// Create the client using these custom settings.try(AreaInsightsClientareaInsightsClient=AreaInsightsClient.create(areaInsightsSettings)){// ...}

Go

areainsightspb"cloud.google.com/go/maps/areainsights/apiv1/areainsightspb""google.golang.org/api/option"// Import the option package when using an API key"google.golang.org/genproto/googleapis/type/latlng"...// Initialize the client, passing the API keyclientOpts:=[]option.ClientOption{option.WithAPIKey("API_KEY")}c,err:=areainsights.NewClient(ctx,clientOpts...)

NodeJS

// Instantiates the Places client, passing the API keyconstareaInsightsClient=newAreaInsightsClient({apiKey:"API_KEY",});

Python

client=areainsights_v1.AreaInsightsAsyncClient(# Instantiates the insights client, passing the API keyclient_options={"api_key":"API_KEY"})

.NET

usingGoogle.Maps.AreaInsights.V1;usingGoogle.Api.Gax.Grpc;usingGrpc.Core;// ...varapiHeader=CallSettings.FromHeader("X-Goog-Api-Key","API_KEY");vardefaultSettings=AreaInsightsSettings.GetDefault();varsettings=newAreaInsightsSettings{// Merge the API key header into the settings for the ComputeInsights method.ComputeInsightsSettings=defaultSettings.ComputeInsightsSettings.MergedWith(apiHeader)};// Create a client builder with the custom settings.AreaInsightsClientBuilderbuilder=newAreaInsightsClientBuilder{Settings=settings,// Use SslCredentials to create a secure channel for API key authentication.ChannelCredentials=newSslCredentials()};// Build the client from the builder.AreaInsightsClientareaInsightsClient=awaitbuilder.BuildAsync();

When you use API keys in your applications, make sure that they are kept secureduring both storage and transmission. Publicly exposing your API keys can leadto unexpected charges on your account.

The examples on this page use Application Default Credentials.

Examples

Compute insights

The following is an example of how to call compute insights using the clientlibrary.

Java

importcom.google.api.core.ApiFuture;importcom.google.maps.areainsights.v1.AreaInsightsClient;importcom.google.maps.areainsights.v1.ComputeInsightsRequest;importcom.google.maps.areainsights.v1.ComputeInsightsResponse;importcom.google.maps.areainsights.v1.Filter;importcom.google.maps.areainsights.v1.Insight;importcom.google.maps.areainsights.v1.LocationFilter;importcom.google.maps.areainsights.v1.OperatingStatus;importcom.google.maps.areainsights.v1.RatingFilter;importcom.google.maps.areainsights.v1.TypeFilter;importcom.google.type.LatLng;// ...publicstaticvoidcallComputeInsights()throwsException{// Use try-with-resources to automatically close the client.try(AreaInsightsClientareaInsightsClient=AreaInsightsClient.create()){// Build the request.ComputeInsightsRequestrequest=ComputeInsightsRequest.newBuilder().addInsights(Insight.INSIGHT_COUNT).setFilter(Filter.newBuilder().setLocationFilter(LocationFilter.newBuilder().setCircle(LocationFilter.Circle.newBuilder().setLatLng(LatLng.newBuilder().setLatitude(37.7749).setLongitude(-122.4194).build()).setRadius(2000).build())).setTypeFilter(TypeFilter.newBuilder().addIncludedTypes("restaurant").build()).addOperatingStatus(OperatingStatus.OPERATING_STATUS_OPERATIONAL).setRatingFilter(RatingFilter.newBuilder().setMinRating(4.2f).build()).build()).build();// Make the API call.ApiFuturefutureResponse=areaInsightsClient.computeInsightsCallable().futureCall(request);ComputeInsightsResponseresponse=futureResponse.get();System.out.println(response);}}

Go

packagemainimport("context""fmt""log"areainsights"cloud.google.com/go/maps/areainsights/apiv1"areainsightspb"cloud.google.com/go/maps/areainsights/apiv1/areainsightspb""google.golang.org/genproto/googleapis/type/latlng")funcmain(){ctx:=context.Background()// Initialize the clientc,err:=areainsights.NewClient(ctx)iferr!=nil{log.Fatalf("NewClient: %v",err)}deferc.Close()minRating:=float32(4.2)// Create the ComputeInsightsRequestreq:=&areainsightspb.ComputeInsightsRequest{Insights:[]areainsightspb.Insight{areainsightspb.Insight_INSIGHT_COUNT,},Filter:&areainsightspb.Filter{LocationFilter:&areainsightspb.LocationFilter{Area:&areainsightspb.LocationFilterCircle{Circle:&areainsightspb.LocationFilter_Circle{Center:&areainsightspb.LocationFilter_Circle_LatLng{LatLng:&latlng.LatLng{Latitude:37.7749,Longitude:-122.4194,},},Radius:2000,},},},TypeFilter:&areainsightspb.TypeFilter{// Filter for restaurantsIncludedTypes:[]string{"restaurant"},},OperatingStatus:[]areainsightspb.OperatingStatus{areainsightspb.OperatingStatus_OPERATING_STATUS_OPERATIONAL,},RatingFilter:&areainsightspb.RatingFilter{MinRating:&minRating,},},}// Call ComputeInsightsresp,err:=c.ComputeInsights(ctx,req)iferr!=nil{log.Fatalf("ComputeInsights failed: %v",err)}// Process the response// Since we requested a count, use the GetCount() method to retrieve the result.count:=resp.GetCount()fmt.Printf("Found a total of %d operational restaurants with a rating of %.1f or higher in the specified area.\n",count,minRating)}

NodeJS

// Imports the libraryconst{AreaInsightsClient,protos}=require('@googlemaps/areainsights');// Instantiates a clientconstareaInsightsClient=newAreaInsightsClient();asyncfunctioncomputeInsights(){// Build the requestconstrequest={insights:[protos.google.maps.areainsights.v1.Insight.INSIGHT_COUNT],filter:{locationFilter:{circle:{latLng:{latitude:37.7749,longitude:-122.4194,},radius:2000,},},typeFilter:{includedTypes:['restaurant'],},operatingStatus:[protos.google.maps.areainsights.v1.OperatingStatus.OPERATING_STATUS_OPERATIONAL,],ratingFilter:{minRating:4.2,},},};// Make the requestconst[response]=awaitareaInsightsClient.computeInsights(request);console.log(response);}computeInsights();

Python

importasynciofromgoogle.mapsimportareainsights_v1fromgoogle.typeimportlatlng_pb2asyncdefplaces_aggregate():client=areainsights_v1.AreaInsightsAsyncClient()# Build the requestrequest=areainsights_v1.ComputeInsightsRequest(insights=[areainsights_v1.Insight.INSIGHT_COUNT],filter=areainsights_v1.Filter(location_filter=areainsights_v1.LocationFilter(circle=areainsights_v1.LocationFilter.Circle(lat_lng=latlng_pb2.LatLng(latitude=37.7749,longitude=-122.4194),radius=2000)),type_filter=areainsights_v1.TypeFilter(included_types=["restaurant"]),operating_status=[areainsights_v1.OperatingStatus.OPERATING_STATUS_OPERATIONAL],rating_filter=areainsights_v1.RatingFilter(min_rating=4.2)))# Make the requestresponse=awaitclient.compute_insights(request=request)returnresponseprint(asyncio.run(places_aggregate()))

.NET

usingGoogle.Maps.AreaInsights.V1;// ...publicstaticasyncTaskGetPlacesAggregateCountAsync(){// Create the client.AreaInsightsClientareaInsightsClient=awaitAreaInsightsClient.CreateAsync();// Build the requestvarrequest=newComputeInsightsRequest{Insights={Insight.Count},Filter=newFilter{LocationFilter=newLocationFilter{// Define the search area as a circle with a 2km radius.Circle=newLocationFilter.Types.Circle{LatLng=newGoogle.Type.LatLng{Latitude=37.7749,Longitude=-122.4194},Radius=2000}},TypeFilter=newTypeFilter{// Filter for places of type "restaurant".IncludedTypes={"restaurant"}},// Filter for places that are operational.OperatingStatus={OperatingStatus.Operational},RatingFilter=newRatingFilter{// Filter for places with a minimum user rating of 4.2.MinRating=4.2f}}};// Make the requestComputeInsightsResponseresponse=awaitareaInsightsClient.ComputeInsightsAsync(request);// Return the count from the response.returnresponse.Count;}

This request returns a count of places within a 2km radius of downtown SanFrancisco that are operational restaurants with a user rating of 4.2 or higher.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-21 UTC.