本部分介绍了向 Places Insights API 发出的一系列示例请求。
返回圆形内的位置
返回伦敦特拉法加广场方圆 200 米范围内的所有餐厅。
- 搜索区域是一个以特定纬度和经度为中心的圆形。此圆形的半径为 200 米,这决定了搜索区域的大小。
- 请求的地点类型为餐厅,此值会使用
typeFilters
中的includedTypes
传递。 - 系统使用
INSIGHTS_COUNT
请求计数,并使用INSIGHTS_PLACES
请求地点 ID。
休息
curl --location 'https://areainsights.googleapis.com/v1:computeInsights' \ --header 'X-Goog-Api-Key: API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "insights": ["INSIGHT_COUNT", "INSIGHT_PLACES"], "filter": { "locationFilter": { "circle": { "latLng": { "latitude": 51.508, "longitude": -0.128}, "radius": 200 } }, "typeFilter": { "includedTypes": "restaurant" } } }'
Python (gRPC)
from google.maps import areainsights_v1 from google.maps.areainsights_v1.types import ( ComputeInsightsRequest, Filter, LocationFilter, TypeFilter, Insight ) from google.type import latlng_pb2 from google.oauth2 import service_account def get_area_insights(): # Initialize the client credentials = service_account.Credentials.from_service_account_file( 'path/to/service_account.json', scopes=['https://www.googleapis.com/auth/cloud-platform'] ) client = areainsights_v1.AreaInsightsClient( credentials=credentials ) # Create location filter with circle lat_lng = latlng_pb2.LatLng( latitude=51.508, longitude=-0.128 ) location_filter = LocationFilter( circle=LocationFilter.Circle( lat_lng=lat_lng, radius=200 ) ) # Create type filter type_filter = TypeFilter( included_types=["restaurant"] ) # Create the main filter filter = Filter( location_filter=location_filter, type_filter=type_filter ) # Create the request request = ComputeInsightsRequest( insights=[ Insight.INSIGHT_COUNT, Insight.INSIGHT_PLACES ], filter=filter ) try: # Make the request response = client.compute_insights(request=request) # Print results print(f"Total count: {response.count}") print("\nPlaces found:") for place in response.place_insights: print(f"Place ID: {place.place}") except Exception as e: print(f"Error occurred: {e}") if __name__ == "__main__": get_area_insights()
排除地点类型
您可以从统计范围中排除地点类型。
以下请求与第一个示例相同,但会向 typeFilters
添加 excludedTypes
。您可以为 includedTypes
和 excludedTypes
使用字符串或字符串数组。
在此示例中,系统会从 restaurant
计数中排除两种地点类型:cafe
和 bakery
。
休息
curl --location 'https://areainsights.googleapis.com/v1:computeInsights' \ --header 'X-Goog-Api-Key: API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "insights": ["INSIGHT_COUNT", "INSIGHT_PLACES"], "filter": { "locationFilter": { "circle": { "latLng": { "latitude": 51.508, "longitude": -0.128}, "radius": 200 } }, "typeFilter": { "includedTypes": "restaurant", "excludedTypes": [ "cafe", "bakery" ] } } }'
Python (gRPC)
from google.maps import areainsights_v1 from google.maps.areainsights_v1.types import ( ComputeInsightsRequest, Filter, LocationFilter, TypeFilter, Insight ) from google.type import latlng_pb2 from google.oauth2 import service_account def get_area_insights(): # Initialize the client with service account credentials = service_account.Credentials.from_service_account_file( 'path/to/service_account.json', scopes=['https://www.googleapis.com/auth/cloud-platform'] ) client = areainsights_v1.AreaInsightsClient( credentials=credentials ) # Create location filter with circle lat_lng = latlng_pb2.LatLng( latitude=51.508, longitude=-0.128 ) location_filter = LocationFilter( circle=LocationFilter.Circle( lat_lng=lat_lng, radius=200 ) ) # Create type filter with both included and excluded types type_filter = TypeFilter( included_types=["restaurant"], excluded_types=["cafe", "bakery"] ) # Create the main filter filter = Filter( location_filter=location_filter, type_filter=type_filter ) # Create the request request = ComputeInsightsRequest( insights=[ Insight.INSIGHT_COUNT, Insight.INSIGHT_PLACES ], filter=filter ) try: # Make the request response = client.compute_insights(request=request) # Print results print(f"Total count: {response.count}") print("\nPlaces found:") for place in response.place_insights: print(f"Place ID: {place.place}") except Exception as e: print(f"Error occurred: {e}") if __name__ == "__main__": get_area_insights()
使用主要类型
此示例修改了第一个示例中的请求,以便在统计数量时仅包含 primaryType
为 restaurant
的地点。
休息
curl --location 'https://areainsights.googleapis.com/v1:computeInsights' \ --header 'X-Goog-Api-Key: API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "insights": ["INSIGHT_COUNT", "INSIGHT_PLACES"], "filter": { "locationFilter": { "circle": { "latLng": { "latitude": 51.508, "longitude": -0.128}, "radius": 200 } }, "typeFilter": { "includedPrimaryTypes": "restaurant" } } }'
Python (gRPC)
from google.maps import areainsights_v1 from google.maps.areainsights_v1.types import ( ComputeInsightsRequest, Filter, LocationFilter, TypeFilter, Insight ) from google.type import latlng_pb2 from google.oauth2 import service_account def get_area_insights(): # Initialize the client with service account credentials = service_account.Credentials.from_service_account_file( 'path/to/service_account.json', scopes=['https://www.googleapis.com/auth/cloud-platform'] ) client = areainsights_v1.AreaInsightsClient( credentials=credentials ) # Create location filter with circle lat_lng = latlng_pb2.LatLng( latitude=51.508, longitude=-0.128 ) location_filter = LocationFilter( circle=LocationFilter.Circle( lat_lng=lat_lng, radius=200 ) ) # Create type filter with primary types type_filter = TypeFilter( included_primary_types=["restaurant"] ) # Create the main filter filter = Filter( location_filter=location_filter, type_filter=type_filter ) # Create the request request = ComputeInsightsRequest( insights=[ Insight.INSIGHT_COUNT, Insight.INSIGHT_PLACES ], filter=filter ) try: # Make the request response = client.compute_insights(request=request) # Print results print(f"Total count: {response.count}") print("\nPlaces found:") for place in response.place_insights: print(f"Place ID: {place.place}") except Exception as e: print(f"Error occurred: {e}") if __name__ == "__main__": get_area_insights()
自定义多边形
此示例演示了如何使用自定义多边形来定义搜索区域。请注意,指定 INSIGHTS_PLACES
会将搜索范围限制在足够小且最多可返回 100 个地点 ID 的区域。对于较大的区域,请使用 INSIGHTS_COUNT
来绕过此限制,以便该服务无需返回各个地点 ID。
与之前一样,使用的地点类型为 restaurant
。此示例还介绍了另外三种过滤器:
operatingStatus
:此示例仅统计运营中的位置。priceLevel
:此示例仅统计价格低廉和价格适中的地点。ratingFilter
:此示例仅统计评分介于 4.0 到 5.0 之间的地点。
休息
curl --location 'https://areainsights.googleapis.com/v1:computeInsights' \ --header 'X-Goog-Api-Key: API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "insights": [ "INSIGHT_COUNT" ], "filter": { "locationFilter": { "customArea": { "polygon": { "coordinates": [ { "latitude": 37.776, "longitude": -122.666 }, { "latitude": 37.130, "longitude": -121.898 }, { "latitude": 37.326, "longitude": -121.598 }, { "latitude": 37.912, "longitude": -122.247 }, { "latitude": 37.776, "longitude": -122.666 } ] } } }, "typeFilter": { "includedTypes": "restaurant" }, "operatingStatus": [ "OPERATING_STATUS_OPERATIONAL" ], "priceLevels": [ "PRICE_LEVEL_INEXPENSIVE", "PRICE_LEVEL_MODERATE" ], "ratingFilter": { "minRating": 4.0, "maxRating": 5.0 } } }'
Python (gRPC)
from google.maps import areainsights_v1 from google.maps.areainsights_v1.types import ( ComputeInsightsRequest, Filter, LocationFilter, TypeFilter, Insight, RatingFilter, OperatingStatus, PriceLevel ) from google.type import latlng_pb2 from google.oauth2 import service_account def get_area_insights(): # Initialize the client with service account credentials = service_account.Credentials.from_service_account_file( 'path/to/service_account.json', scopes=['https://www.googleapis.com/auth/cloud-platform'] ) client = areainsights_v1.AreaInsightsClient( credentials=credentials ) # Create coordinates for the polygon coordinates = [ latlng_pb2.LatLng(latitude=37.776, longitude=-122.666), latlng_pb2.LatLng(latitude=37.130, longitude=-121.898), latlng_pb2.LatLng(latitude=37.326, longitude=-121.598), latlng_pb2.LatLng(latitude=37.912, longitude=-122.247), latlng_pb2.LatLng(latitude=37.776, longitude=-122.666) # Closing point ] # Create custom area with polygon using the nested structure location_filter = LocationFilter( custom_area=LocationFilter.CustomArea( polygon=LocationFilter.CustomArea.Polygon(coordinates=coordinates) ) ) # Create type filter type_filter = TypeFilter( included_types=["restaurant"] ) # Create rating filter rating_filter = RatingFilter( min_rating=4.0, max_rating=5.0 ) # Create the main filter filter = Filter( location_filter=location_filter, type_filter=type_filter, operating_status=[OperatingStatus.OPERATING_STATUS_OPERATIONAL], price_levels=[ PriceLevel.PRICE_LEVEL_INEXPENSIVE, PriceLevel.PRICE_LEVEL_MODERATE ], rating_filter=rating_filter ) # Create the request request = ComputeInsightsRequest( insights=[Insight.INSIGHT_COUNT], filter=filter ) try: # Make the request response = client.compute_insights(request=request) # Print results print(f"Total count: {response.count}") except Exception as e: print(f"Error occurred: {e}") if __name__ == "__main__": get_area_insights()
地理区域
此示例使用地理区域地点 ID 设置搜索区域。这些地点 ID 包含地点(例如镇或城市)的几何图形。此处使用的地点 ID 为 ChIJiQHsW0m3j4ARm69rRkrUF3w
,对应于加利福尼亚州山景城。
将地点 ID 传递给 Places Insights API 会将搜索区域设置为相应地理区域的边界。地点 ID 使用 place
传递,格式为 places/place_ID
。
您可以通过以下任一方式获取地理区域地点 ID:
休息
curl --location 'https://areainsights.googleapis.com/v1:computeInsights' \ --header 'X-Goog-Api-Key: API_KEY' \ --header 'Content-Type: application/json' \ --data '{ "insights": [ "INSIGHT_COUNT" ], "filter": { "locationFilter": { "region": { "place": "places/ChIJiQHsW0m3j4ARm69rRkrUF3w" } }, "typeFilter": { "includedTypes": [ "restaurant" ] } } }'
Python (gRPC)
from google.maps import areainsights_v1 from google.maps.areainsights_v1.types import ( ComputeInsightsRequest, Filter, LocationFilter, TypeFilter, Insight ) from google.oauth2 import service_account def get_area_insights(): # Initialize the client with service account credentials = service_account.Credentials.from_service_account_file( 'path/to/service_account.json', scopes=['https://www.googleapis.com/auth/cloud-platform'] ) client = areainsights_v1.AreaInsightsClient( credentials=credentials ) # Create location filter with region location_filter = LocationFilter( region=LocationFilter.Region( place="places/ChIJiQHsW0m3j4ARm69rRkrUF3w" ) ) # Create type filter type_filter = TypeFilter( included_types=["restaurant"] ) # Create the main filter filter = Filter( location_filter=location_filter, type_filter=type_filter ) # Create the request request = ComputeInsightsRequest( insights=[Insight.INSIGHT_COUNT], filter=filter ) try: # Make the request response = client.compute_insights(request=request) # Print results print(f"Total count: {response.count}") except Exception as e: print(f"Error occurred: {e}") if __name__ == "__main__": get_area_insights()