本節將介紹向 Places Insights API 提出的一系列要求範例。
傳回圓內的景點
傳回倫敦特拉法加廣場 (Trafalgar Square) 方圓 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()
排除地點類型
您可以從計數中排除地點類型。
以下要求與第一個範例相同,但會將 excludedTypes
新增至 typeFilters
。您可以為 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()