Beispiele

In diesem Abschnitt finden Sie eine Reihe von Beispielanfragen an die Places Insights API.

Orte innerhalb eines Kreises zurückgeben

Gibt alle Restaurants in einem Umkreis von 200 m um den Trafalgar Square in London zurück.

  • Der Suchbereich ist ein Kreis mit einem bestimmten Breiten- und Längengrad als Mittelpunkt. Der Radius dieses Kreises beträgt 200 Meter und bestimmt die Größe des Suchgebiets.
  • Der angeforderte Ortstyp ist „Restaurant“ und wird in typeFilters mit includedTypes übergeben.
  • Die Anzahl wird mit INSIGHTS_COUNT angefordert und die Orts-IDs mit INSIGHTS_PLACES.

Ausruhen

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()
  

Ortstypen ausschließen

Sie können bestimmte Standorttypen von der Zählung ausschließen.

Die folgende Anfrage entspricht dem ersten Beispiel, aber excludedTypes wird zu typeFilters hinzugefügt. Sie können für includedTypes und excludedTypes entweder einen String oder ein String-Array verwenden.

In diesem Beispiel werden zwei Ortstypen – cafe und bakery – von der Anzahl der restaurant ausgeschlossen.

Ausruhen

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()
  

Primären Typ verwenden

In diesem Beispiel wird die Anfrage aus dem ersten Beispiel so geändert, dass nur Orte mit einer primaryType von restaurant in die Zählung einbezogen werden.

Ausruhen

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()
  

Benutzerdefiniertes Polygon

In diesem Beispiel wird gezeigt, wie Sie mit einem benutzerdefinierten Polygon Ihren Suchbereich definieren. Wenn Sie INSIGHTS_PLACES angeben, wird die Suche auf Gebiete beschränkt, die klein genug sind, um bis zu 100 Orts-IDs zurückzugeben. Verwenden Sie für größere Gebiete INSIGHTS_COUNT, um diese Einschränkung zu umgehen, damit der Dienst keine einzelnen Orts-IDs zurückgeben muss.

Wie bereits erwähnt, ist der verwendete Ortstyp restaurant. In diesem Beispiel werden außerdem drei weitere Filter vorgestellt:

  • operatingStatus: In diesem Beispiel werden nur betriebsbereite Orte gezählt.
  • priceLevel: In diesem Beispiel werden nur Orte mit günstigen und durchschnittlichen Preisen gezählt.
  • ratingFilter: In diesem Beispiel werden nur Orte mit einer Bewertung zwischen 4,0 und 5,0 gezählt.

Ausruhen

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()
    
  

Geografische Region

In diesem Beispiel wird eine Orts-ID für ein geografisches Gebiet verwendet, um das Suchgebiet festzulegen. Diese Orts-IDs enthalten die Geometrie eines Orts, z. B. einer Stadt. Die hier verwendete Orts-ID ist ChIJiQHsW0m3j4ARm69rRkrUF3w, was der Stadt Mountain View, Kalifornien entspricht.

Wenn Sie die Orts-ID an die Places Insights API übergeben, wird der Suchbereich auf die Grenzen des geografischen Gebiets festgelegt. Die Orts-ID wird mit place im Format places/place_ID übergeben.

Sie haben folgende Möglichkeiten, eine Orts-ID für ein geografisches Gebiet zu erhalten:

Ausruhen

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()