Places Aggregate API client library examples

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

Install the client libraries

See Places Aggregate API client libraries for installation instructions.

Authentication

When you use client libraries, you use Application Default Credentials (ADC) to authenticate. For information about setting up ADC, see Provide credentials for Application Default Credentials . For information about using ADC with client libraries, see Authenticate using client libraries .

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

Java

 import 
  
 java.util.HashMap 
 ; 
 import 
  
 java.util.Map 
 ; 
 import 
  
 com.google.api.gax.core.NoCredentialsProvider 
 ; 
 import 
  
 com.google.api.gax.rpc.FixedHeaderProvider 
 ; 
 import 
  
 com.google.api.gax.rpc.HeaderProvider 
 ; 
 import 
  
 com.google.maps.areainsights.v1.AreaInsightsClient 
 ; 
 import 
  
 com.google.maps.areainsights.v1.AreaInsightsSettings 
 ; 
 // ... 
 String 
  
 apiKey 
  
 = 
  
 "API_KEY" 
 ; 
 // Create a HeaderProvider to inject the API key into every request. 
 Map 
 < 
 String 
 , 
  
 String 
 > 
  
 headers 
  
 = 
  
 new 
  
 HashMap 
<> (); 
 headers 
 . 
 put 
 ( 
 "x-goog-api-key" 
 , 
  
 apiKey 
 ); 
 HeaderProvider 
  
 headerProvider 
  
 = 
  
 FixedHeaderProvider 
 . 
 create 
 ( 
 headers 
 ); 
 // Configure the client settings. 
 AreaInsightsSettings 
  
 areaInsightsSettings 
  
 = 
  
 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 
  
 ( 
 AreaInsightsClient 
  
 areaInsightsClient 
  
 = 
  
 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 key 
 clientOpts 
  
 := 
  
 [] 
 option 
 . 
 ClientOption 
 { 
 option 
 . 
 WithAPIKey 
 ( 
 "API_KEY" 
 )} 
 c 
 , 
  
 err 
  
 := 
  
 areainsights 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 clientOpts 
 ... 
 ) 

NodeJS

 // Instantiates the Places client, passing the API key 
 const 
  
 areaInsightsClient 
  
 = 
  
 new 
  
 AreaInsightsClient 
 ({ 
  
 apiKey 
 : 
  
 "API_KEY" 
 , 
 }); 

Python

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

.NET

 using 
  
 Google.Maps.AreaInsights.V1 
 ; 
 using 
  
 Google.Api.Gax.Grpc 
 ; 
 using 
  
 Grpc.Core 
 ; 
 // ... 
 var 
  
 apiHeader 
  
 = 
  
 CallSettings 
 . 
 FromHeader 
 ( 
 "X-Goog-Api-Key" 
 , 
  
 "API_KEY" 
 ); 
 var 
  
 defaultSettings 
  
 = 
  
 AreaInsightsSettings 
 . 
 GetDefault 
 (); 
 var 
  
 settings 
  
 = 
  
 new 
  
 AreaInsightsSettings 
 { 
  
 // 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. 
 AreaInsightsClientBuilder 
  
 builder 
  
 = 
  
 new 
  
 AreaInsightsClientBuilder 
 { 
  
 Settings 
  
 = 
  
 settings 
 , 
  
 // Use SslCredentials to create a secure channel for API key authentication. 
  
 ChannelCredentials 
  
 = 
  
 new 
  
 SslCredentials 
 () 
 }; 
 // Build the client from the builder. 
 AreaInsightsClient 
  
 areaInsightsClient 
  
 = 
  
 await 
  
 builder 
 . 
 BuildAsync 
 (); 

When you use API keys in your applications, make sure that they are kept secure during both storage and transmission. Publicly exposing your API keys can lead to 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 client library.

Java

 import 
  
 com.google.api.core.ApiFuture 
 ; 
 import 
  
 com.google.maps.areainsights.v1.AreaInsightsClient 
 ; 
 import 
  
 com.google.maps.areainsights.v1.ComputeInsightsRequest 
 ; 
 import 
  
 com.google.maps.areainsights.v1.ComputeInsightsResponse 
 ; 
 import 
  
 com.google.maps.areainsights.v1.Filter 
 ; 
 import 
  
 com.google.maps.areainsights.v1.Insight 
 ; 
 import 
  
 com.google.maps.areainsights.v1.LocationFilter 
 ; 
 import 
  
 com.google.maps.areainsights.v1.OperatingStatus 
 ; 
 import 
  
 com.google.maps.areainsights.v1.RatingFilter 
 ; 
 import 
  
 com.google.maps.areainsights.v1.TypeFilter 
 ; 
 import 
  
 com.google.type.LatLng 
 ; 
 // ... 
 public 
  
 static 
  
 void 
  
 callComputeInsights 
 () 
  
 throws 
  
 Exception 
  
 { 
  
 // Use try-with-resources to automatically close the client. 
  
 try 
  
 ( 
 AreaInsightsClient 
  
 areaInsightsClient 
  
 = 
  
 AreaInsightsClient 
 . 
 create 
 ()) 
  
 { 
  
 // Build the request. 
  
 ComputeInsightsRequest 
  
 request 
  
 = 
  
 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. 
  
 ApiFuture 
   
 futureResponse 
  
 = 
  
 areaInsightsClient 
 . 
 computeInsightsCallable 
 (). 
 futureCall 
 ( 
 request 
 ); 
  
 ComputeInsightsResponse 
  
 response 
  
 = 
  
 futureResponse 
 . 
 get 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 response 
 ); 
  
 } 
 } 
 

Go

 package 
  
 main 
 import 
  
 ( 
  
 "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" 
 ) 
 func 
  
 main 
 () 
  
 { 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 // Initialize the client 
  
 c 
 , 
  
 err 
  
 := 
  
 areainsights 
 . 
 NewClient 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "NewClient: %v" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 c 
 . 
 Close 
 () 
  
 minRating 
  
 := 
  
 float32 
 ( 
 4.2 
 ) 
  
 // Create the ComputeInsightsRequest 
  
 req 
  
 := 
  
& areainsightspb 
 . 
 ComputeInsightsRequest 
 { 
  
 Insights 
 : 
  
 [] 
 areainsightspb 
 . 
 Insight 
 { 
  
 areainsightspb 
 . 
 Insight_INSIGHT_COUNT 
 , 
  
 }, 
  
 Filter 
 : 
  
& areainsightspb 
 . 
 Filter 
 { 
  
 LocationFilter 
 : 
  
& areainsightspb 
 . 
 LocationFilter 
 { 
  
 Area 
 : 
  
& areainsightspb 
 . 
 LocationFilter Circle 
 
 { 
  
 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 restaurants 
  
 IncludedTypes 
 : 
  
 [] 
 string 
 { 
 "restaurant" 
 }, 
  
 }, 
  
 OperatingStatus 
 : 
  
 [] 
 areainsightspb 
 . 
 OperatingStatus 
 { 
  
 areainsightspb 
 . 
 OperatingStatus_OPERATING_STATUS_OPERATIONAL 
 , 
  
 }, 
  
 RatingFilter 
 : 
  
& areainsightspb 
 . 
 RatingFilter 
 { 
  
 MinRating 
 : 
  
& minRating 
 , 
  
 }, 
  
 }, 
  
 } 
  
 // Call ComputeInsights 
  
 resp 
 , 
  
 err 
  
 := 
  
 c 
 . 
 ComputeInsights 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 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 library 
 const 
  
 { 
 AreaInsightsClient 
 , 
  
 protos 
 } 
  
 = 
  
 require 
 ( 
 '@googlemaps/areainsights' 
 ); 
 // Instantiates a client 
 const 
  
 areaInsightsClient 
  
 = 
  
 new 
  
 AreaInsightsClient 
 (); 
 async 
  
 function 
  
 computeInsights 
 () 
  
 { 
  
 // Build the request 
  
 const 
  
 request 
  
 = 
  
 { 
  
 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 request 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 areaInsightsClient 
 . 
 computeInsights 
 ( 
 request 
 ); 
  
 console 
 . 
 log 
 ( 
 response 
 ); 
 } 
 computeInsights 
 (); 

Python

 import 
  
 asyncio 
 from 
  
 google.maps 
  
 import 
 areainsights_v1 
 from 
  
 google.type 
  
 import 
 latlng_pb2 
 async 
 def 
  
 places_aggregate 
 (): 
 client 
 = 
 areainsights_v1 
 . 
 AreaInsightsAsyncClient 
 () 
 # Build the request 
 request 
 = 
 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 request 
 response 
 = 
 await 
 client 
 . 
 compute_insights 
 ( 
 request 
 = 
 request 
 ) 
 return 
 response 
 print 
 ( 
 asyncio 
 . 
 run 
 ( 
 places_aggregate 
 ())) 

.NET

 using 
  
 Google.Maps.AreaInsights.V1 
 ; 
 // ... 
 public 
  
 static 
  
 async 
  
 Task 
   
 GetPlacesAggregateCountAsync 
 () 
 { 
  
 // Create the client. 
  
 AreaInsightsClient 
  
 areaInsightsClient 
  
 = 
  
 await 
  
 AreaInsightsClient 
 . 
 CreateAsync 
 (); 
  
 // Build the request 
  
 var 
  
 request 
  
 = 
  
 new 
  
 ComputeInsightsRequest 
  
 { 
  
 Insights 
  
 = 
  
 { 
  
 Insight 
 . 
 Count 
  
 }, 
  
 Filter 
  
 = 
  
 new 
  
 Filter 
  
 { 
  
 LocationFilter 
  
 = 
  
 new 
  
 LocationFilter 
  
 { 
  
 // Define the search area as a circle with a 2km radius. 
  
 Circle 
  
 = 
  
 new 
  
 LocationFilter 
 . 
 Types 
 . 
 Circle 
  
 { 
  
 LatLng 
  
 = 
  
 new 
  
 Google 
 . 
 Type 
 . 
 LatLng 
  
 { 
  
 Latitude 
  
 = 
  
 37.7749 
 , 
  
 Longitude 
  
 = 
  
 - 
 122.4194 
  
 }, 
  
 Radius 
  
 = 
  
 2000 
  
 } 
  
 }, 
  
 TypeFilter 
  
 = 
  
 new 
  
 TypeFilter 
  
 { 
  
 // Filter for places of type "restaurant". 
  
 IncludedTypes 
  
 = 
  
 { 
  
 "restaurant" 
  
 } 
  
 }, 
  
 // Filter for places that are operational. 
  
 OperatingStatus 
  
 = 
  
 { 
  
 OperatingStatus 
 . 
 Operational 
  
 }, 
  
 RatingFilter 
  
 = 
  
 new 
  
 RatingFilter 
  
 { 
  
 // Filter for places with a minimum user rating of 4.2. 
  
 MinRating 
  
 = 
  
 4.2f 
  
 } 
  
 } 
  
 }; 
  
 // Make the request 
  
 ComputeInsightsResponse 
  
 response 
  
 = 
  
 await 
  
 areaInsightsClient 
 . 
 ComputeInsightsAsync 
 ( 
 request 
 ); 
  
 // Return the count from the response. 
  
 return 
  
 response 
 . 
 Count 
 ; 
 } 
 

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

Create a Mobile Website
View Site in Mobile | Classic
Share by: