ส่วนนี้จะแสดงตัวอย่างคําขอไปยัง Places Insights API
แสดงสถานที่ภายในวงกลม
แสดงร้านอาหารทั้งหมดในรัศมี 200 เมตรจาก Trafalgar Square, London
- พื้นที่การค้นหาคือวงกลมที่มีศูนย์กลางอยู่ที่ละติจูดและลองจิจูดที่เฉพาะเจาะจง รัศมีของวงกลมนี้คือ 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" } } }'
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" ] } } }'
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" } } }'
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 } } }'
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 Insights 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" ] } } }'
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()