ส่วนนี้จะครอบคลุมชุดคำขอตัวอย่างไปยัง Places Aggregate API
แสดงสถานที่ภายในวงกลม
แสดงร้านอาหารทั้งหมดในรัศมี 200 เมตรจากจัตุรัสทราฟัลการ์ กรุงลอนดอน
- พื้นที่ค้นหาเป็นวงกลมที่มีจุดศูนย์กลางอยู่ที่ละติจูดและลองจิจูดที่เฉพาะเจาะจง รัศมีของวงกลมนี้คือ 200 เมตร ซึ่งกำหนดขนาดของ พื้นที่ค้นหา
- ประเภทสถานที่ที่ขอคือร้านอาหาร และจะส่งผ่านโดยใช้
includedTypes
ภายในtypeFilters
- ระบบจะขอจำนวนโดยใช้
INSIGHTS_COUNT
และขอรหัสสถานที่โดยใช้INSIGHTS_PLACES
พักผ่อน
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
ได้
ตัวอย่างนี้ยกเว้นสถานที่ 2 ประเภท ได้แก่ cafe
และ bakery
จากจำนวน 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": { "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 รายการ สำหรับพื้นที่ขนาดใหญ่ ให้ใช้
INSIGHTS_COUNT
เพื่อข้ามข้อจำกัดนี้เพื่อให้บริการไม่ต้อง
แสดงรหัสสถานที่แต่ละรายการ
เช่นเดียวกับก่อนหน้านี้ ประเภทสถานที่ที่ใช้คือ restaurant
ตัวอย่างนี้ยังแสดงตัวกรองอื่นๆ อีก 3 รายการด้วย
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()
พื้นที่ทางภูมิศาสตร์
ตัวอย่างนี้ใช้รหัสสถานที่ของพื้นที่ทางภูมิศาสตร์เพื่อตั้งค่าพื้นที่ค้นหา
รหัสสถานที่เหล่านี้รวมถึงรูปทรงเรขาคณิตของสถานที่ เช่น เมือง รหัสสถานที่ที่ใช้ในที่นี้คือ ChIJiQHsW0m3j4ARm69rRkrUF3w
ซึ่งสอดคล้องกับ
เมืองเมาน์เทนวิว รัฐแคลิฟอร์เนีย
การส่งรหัสสถานที่ไปยัง Places Aggregate API จะตั้งค่าพื้นที่ค้นหาเป็นขอบเขต
ของพื้นที่ทางภูมิศาสตร์ ระบบจะส่งรหัสสถานที่โดยใช้ place
ในรูปแบบ
places/place_ID
คุณรับรหัสสถานที่ของพื้นที่ทางภูมิศาสตร์ได้ด้วยวิธีใดวิธีหนึ่งต่อไปนี้
- เครื่องมือค้นหารหัสสถานที่
- Geocoding API
- การค้นหาด้วยข้อความ (ใหม่)
- การค้นหาในบริเวณใกล้เคียง (ใหม่)
- Address Validation API
- Place Autocomplete
พักผ่อน
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()