דוגמאות

בקטע הזה נספק כמה דוגמאות לבקשות ל-Places Insights 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.

בדוגמה הזו, שני סוגי מקומות – 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. בדוגמה הזו מוצגים גם שלושה מסננים נוספים:

  • 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, והוא תואם לעיר Mountain View, California.

העברת מזהה המקום ל-Places Insights API מגדירה את אזור החיפוש לפי גבולות האזור הגיאוגרפי. מזהה המקום מועבר באמצעות place, בפורמט places/place_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()