附近搜索(新)

请选择平台: Android iOS JavaScript 网络服务
欧洲经济区 (EEA) 开发者

简介

附近搜索(新)请求可采用一个或多个地点类型,并返回指定区域内匹配的地点的列表。必须提供指定一种或多种数据类型的字段掩码。Nearby Search(新)仅支持 POST 请求。

借助 API Explorer,您可以发出实时请求,从而熟悉 API 和 API 选项:

不妨试用互动式演示,看看在地图上显示的附近搜索(新)结果。

“附近搜索(新)”请求

附近搜索(新)请求是向以下格式的网址发出的 HTTP POST 请求:

https://places.googleapis.com/v1/places:searchNearby

在 JSON 请求正文或标头中传递所有参数,作为 POST 请求的一部分。例如:

curl -X POST -d '{
  "includedTypes": ["restaurant"],
  "maxResultCount": 10,
  "locationRestriction": {
    "circle": {
      "center": {
        "latitude": 37.7937,
        "longitude": -122.3965},
      "radius": 500.0
    }
  }
}' \
-H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" \
-H "X-Goog-FieldMask: places.displayName" \
https://places.googleapis.com/v1/places:searchNearby

“附近搜索(新)”响应

附近搜索(新)会返回 JSON 对象作为响应。在回答中:

  • places 数组包含所有匹配的地点。
  • 数组中的每个地点都由一个 Place 对象表示。Place 对象包含单个地点的详细信息。
  • 请求中传递的 FieldMask 指定了 Place 对象中返回的字段列表。

完整的 JSON 对象采用以下格式:

{
  "places": [
    {
      object (Place)
    }
  ]
}

必需参数

  • FieldMask

    通过创建响应字段掩码来指定要在响应中返回的字段列表。 使用网址参数 $fieldsfields,或者使用 HTTP 标头 X-Goog-FieldMask,将响应字段掩码传递给相应方法。响应中没有默认的返回字段列表。 如果您省略字段掩码,该方法会返回错误。

    使用字段遮盖是一种良好的设计做法,可确保您不会请求不必要的数据,这有助于避免产生不必要的处理时间和结算费用。

    指定要返回的地点数据类型的逗号分隔列表。例如,检索地点的显示名称和地址。

    X-Goog-FieldMask: places.displayName,places.formattedAddress

    使用 * 可检索所有字段。

    X-Goog-FieldMask: *

    指定以下一个或多个字段:

    • 以下字段会触发附近搜索专业版 SKU

      places.accessibilityOptions
      places.addressComponents
      places.addressDescriptor*
      places.adrFormatAddress
      places.attributions
      places.businessStatus
      places.containingPlaces
      places.displayName
      places.formattedAddress
      places.googleMapsLinks
      places.googleMapsUri
      places.iconBackgroundColor
      places.iconMaskBaseUri
      places.id
      places.location
      places.name**
      places.photos
      places.plusCode
      places.postalAddress
      places.primaryType
      places.primaryTypeDisplayName
      places.pureServiceAreaBusiness
      places.shortFormattedAddress
      places.subDestinations
      places.types
      places.utcOffsetMinutes
      places.viewport

      * 地址描述符已面向印度客户正式发布,但在其他国家/地区仍处于实验阶段。

      ** places.name 字段包含地点资源名称,格式为:places/PLACE_ID使用 places.displayName 访问地点的文本名称。

    • 以下字段会触发附近搜索企业版 SKU

      places.currentOpeningHours
      places.currentSecondaryOpeningHours
      places.internationalPhoneNumber
      places.nationalPhoneNumber
      places.priceLevel
      places.priceRange
      places.rating
      places.regularOpeningHours
      places.regularSecondaryOpeningHours
      places.userRatingCount
      places.websiteUri

    • 以下字段会触发附近搜索企业版 + 氛围 SKU

      places.allowsDogs
      places.curbsidePickup
      places.delivery
      places.dineIn
      places.editorialSummary
      places.evChargeAmenitySummary
      places.evChargeOptions
      places.fuelOptions
      places.generativeSummary
      places.goodForChildren
      places.goodForGroups
      places.goodForWatchingSports
      places.liveMusic
      places.menuForChildren
      places.neighborhoodSummary
      places.parkingOptions
      places.paymentOptions
      places.outdoorSeating
      places.reservable
      places.restroom
      places.reviews
      places.reviewSummary
      routingSummaries*
      places.servesBeer
      places.servesBreakfast
      places.servesBrunch
      places.servesCocktails
      places.servesCoffee
      places.servesDessert
      places.servesDinner
      places.servesLunch
      places.servesVegetarianFood
      places.servesWine
      places.takeout

      * 仅限文本搜索和附近搜索

  • locationRestriction

    要搜索的区域,以圆形表示,由中心点和半径(以米为单位)定义。 半径必须介于 0.0 到 50000.0 之间(含边界值)。默认半径为 0.0。您必须在请求中将其设置为大于 0.0 的值。

    例如:

    "locationRestriction": {
      "circle": {
        "center": {
          "latitude": 37.7937,
          "longitude": -122.3965
        },
        "radius": 500.0
      }
    }

可选参数

  • includedTypes/excludedTypes、includedPrimaryTypes/excludedPrimaryTypes

    用于指定表 A 中的类型列表,以过滤搜索结果。在每个类型限制类别中,最多可以指定 50 种类型。

    一个地点只能有一个与它关联的主要类型(来自表 A 中的类型)。例如,主要类型可以是 "mexican_restaurant""steak_house"。使用 includedPrimaryTypesexcludedPrimaryTypes 可按地点的主要类型过滤结果。

    一个地点还可以具有与类型 表 A 关联的多个类型值。例如,某餐厅可能具有以下类型:"seafood_restaurant""restaurant""food""point_of_interest""establishment"。使用 includedTypesexcludedTypes 可过滤与地点关联的类型列表中的结果。

    如果您指定了常规的主要类型,例如 "restaurant""hotel",则响应可以包含主要类型比指定类型更具体的地点。例如,您指定要包含主要类型 "restaurant"。然后,响应可以包含主要类型为 "restaurant" 的地点,但也可以包含主要类型更具体的地点,例如 "chinese_restaurant""seafood_restaurant"

    如果搜索指定了多个类型限制,则仅返回满足所有限制的地点。例如,如果您指定 {"includedTypes": ["restaurant"], "excludedPrimaryTypes": ["steak_house"]},则返回的地点会提供 "restaurant" 相关服务,但主要不是作为 "steak_house" 运营的。

    includedTypes

    以英文逗号分隔的地点类型列表(来自表 A),用于搜索。 如果省略此参数,则系统会返回所有类型的地点。

    excludedTypes

    要从搜索中排除的地点类型(以英文逗号分隔),请参阅表 A

    如果您在请求中同时指定了 includedTypes(例如 "school")和 excludedTypes(例如 "primary_school"),则响应中会包含归类为 "school" 但未归类为 "primary_school" 的地点。响应中包含符合以下条件的地点:至少一个 includedTypes没有 excludedTypes

    如果存在任何冲突的类型(例如,某个类型同时出现在 includedTypesexcludedTypes 中),则返回 INVALID_REQUEST 错误。

    includedPrimaryTypes

    以英文逗号分隔的主要地点类型列表(来自表 A),用于在搜索中包含这些地点类型。

    excludedPrimaryTypes

    以英文逗号分隔的表 A 中的主要地点类型列表,用于从搜索中排除这些地点类型。

    如果存在任何冲突的主要类型(例如,某个类型同时出现在 includedPrimaryTypesexcludedPrimaryTypes 中),则会返回 INVALID_ARGUMENT 错误。

  • languageCode

    返回结果所用的语言。

    • 请参阅支持的语言列表。Google 会经常更新支持的语言,因此该列表可能并非详尽无遗。
    • 如果未提供 languageCode,API 会默认使用 en。如果您指定的语言代码无效,API 会返回 INVALID_ARGUMENT 错误。
    • 该 API 会尽力提供用户和当地人都能看懂的街道地址。为了实现这一目标,它会返回本地语言的街道地址,并在必要时根据首选语言将地址音译为用户可读的文字。所有其他地址均以首选语言返回。地址组成部分全部以同一种语言返回,该语言是从第一个组成部分中选择的。
    • 如果名称在首选语言中没有对应项,API 会使用最接近的匹配项。
    • 首选语言对 API 选择返回的结果集以及返回结果的顺序有一定影响。地理编码器会根据语言以不同的方式解读缩写,例如街道类型的缩写,或者在一种语言中有效但在另一种语言中无效的同义词。
  • maxResultCount

    指定要返回的地点结果数上限。必须介于 1 到 20(默认值)之间(含边界值)。

  • rankPreference

    要使用的排名类型。如果省略此参数,结果将按热门程度排序。 可以是以下值之一:

    • POPULARITY(默认)根据结果的热门程度对其进行排序。
    • DISTANCE 根据结果与指定位置之间的距离按升序对结果进行排序。
  • regionCode

    用于设置响应格式的地区代码,以 双字符 CLDR 代码值指定。没有默认值。

    如果响应中 formattedAddress 字段的国家/地区名称与 regionCode 匹配,则从 formattedAddress 中省略国家/地区代码。 此参数对 adrFormatAddress(始终包含国家/地区名称)或 shortFormattedAddress(从不包含国家/地区名称)没有影响。

    大多数 CLDR 代码都与 ISO 3166-1 代码完全一致,但也有一些明显的例外情况。例如,英国的 ccTLD 为“uk”(.co.uk),而其 ISO 3166-1 代码为“gb”(从技术上讲,是指“大不列颠及北爱尔兰联合王国”这一实体)。 此参数可能会根据适用法律影响结果。

“附近搜索”(新)示例

查找某类地点

以下示例展示了“附近搜索”(新)请求,用于获取半径 500 米(由 circle 定义)范围内的所有餐厅的显示名称:

curl -X POST -d '{
  "includedTypes": ["restaurant"],
  "maxResultCount": 10,
  "locationRestriction": {
    "circle": {
      "center": {
        "latitude": 37.7937,
        "longitude": -122.3965},
      "radius": 500.0
    }
  }
}' \
-H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" \
-H "X-Goog-FieldMask: places.displayName" \
https://places.googleapis.com/v1/places:searchNearby

请注意,X-Goog-FieldMask 标头指定了响应包含以下数据字段:places.displayName。 然后,响应的格式如下:

{
  "places": [
    {
      "displayName": {
        "text": "La Mar Cocina Peruana",
        "languageCode": "en"
      }
    },
    {
      "displayName": {
        "text": "Kokkari Estiatorio",
        "languageCode": "en"
      }
    },
    {
      "displayName": {
        "text": "Harborview Restaurant & Bar",
        "languageCode": "en"
      }
    },
...
}

向字段掩码添加更多数据类型,以返回更多信息。 例如,添加 places.formattedAddress,places.types,places.websiteUri 可在回答中包含餐厅地址、类型和网址:

curl -X POST -d '{
  "includedTypes": ["restaurant"],
  "maxResultCount": 10,
  "locationRestriction": {
    "circle": {
      "center": {
        "latitude": 37.7937,
        "longitude": -122.3965},
      "radius": 500.0
    }
  }
}' \
-H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" \
-H "X-Goog-FieldMask: places.displayName,places.formattedAddress,places.types,places.websiteUri" \
https://places.googleapis.com/v1/places:searchNearby

响应现在采用以下格式:

{
  "places": [
    {
      "types": [
        "seafood_restaurant",
        "restaurant",
        "food",
        "point_of_interest",
        "establishment"
      ],
      "formattedAddress": "PIER 1 1/2 The Embarcadero N, San Francisco, CA 94105, USA",
      "websiteUri": "http://lamarsf.com/",
      "displayName": {
        "text": "La Mar Cocina Peruana",
        "languageCode": "en"
      }
    },
    {
      "types": [
        "greek_restaurant",
        "meal_takeaway",
        "restaurant",
        "food",
        "point_of_interest",
        "establishment"
      ],
      "formattedAddress": "200 Jackson St, San Francisco, CA 94111, USA",
      "websiteUri": "https://kokkari.com/",
      "displayName": {
        "text": "Kokkari Estiatorio",
        "languageCode": "en"
      }
    },
...
}

查找多种类型的地点

以下示例展示了附近的搜索(新)请求,用于获取指定 circle 周围 1,000 米半径范围内的所有便利店和酒类商店的显示名称:

curl -X POST -d '{
  "includedTypes": ["liquor_store", "convenience_store"],
  "maxResultCount": 10,
  "locationRestriction": {
    "circle": {
      "center": {
        "latitude": 37.7937,
        "longitude": -122.3965
      },
      "radius": 1000.0
    }
  }
}' \
-H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" \
-H "X-Goog-FieldMask: places.displayName,places.primaryType,places.types" \
https://places.googleapis.com/v1/places:searchNearby
此示例向字段掩码添加了 places.primaryTypeplaces.types,以便响应包含有关每个地点的类型信息,从而更轻松地从结果中选择合适的地名。

以下示例展示了针对类型为 "school" 的所有地点(排除类型为 "primary_school" 的所有地点)的附近搜索(新)请求,并按距离对结果进行排名:

curl -X POST -d '{
  "includedTypes": ["school"],
  "excludedTypes": ["primary_school"],
  "maxResultCount": 10,
  "locationRestriction": {
    "circle": {
      "center": {
        "latitude": 37.7937,
        "longitude": -122.3965
      },
      "radius": 1000.0
    }
  },
  "rankPreference": "DISTANCE"
}' \
-H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" \
-H "X-Goog-FieldMask: places.displayName" \
https://places.googleapis.com/v1/places:searchNearby

搜索某个地区附近的所有地点,并按距离进行排名

以下示例展示了针对旧金山市中心某点附近地点的附近搜索(新)请求。在此示例中,您添加了 rankPreference 参数,以便按距离对结果进行排名:

curl -X POST -d '{
  "maxResultCount": 10,
  "rankPreference": "DISTANCE",
  "locationRestriction": {
    "circle": {
      "center": {
        "latitude": 37.7937,
        "longitude": -122.3965
      },
      "radius": 1000.0
    }
  }
}' \
-H 'Content-Type: application/json' -H "X-Goog-Api-Key: API_KEY" \
-H "X-Goog-FieldMask: places.displayName" \
https://places.googleapis.com/v1/places:searchNearby

获取地址描述符

地址描述符提供有关地点位置的关系信息,包括附近的地标和包含的区域。

以下示例展示了针对圣何塞某购物中心附近地点的附近搜索(新)请求。在此示例中,您在字段掩码中包含 addressDescriptors

curl -X POST -d '{
  "maxResultCount": 5,
  "locationRestriction": {
    "circle": {
      "center": {
        "latitude": 37.321328,
        "longitude": -121.946275
      },"radius": 1000
    }
  },
  "includedTypes": ["restaurant", "cafe"],
  "excludedTypes": [],
  "rankPreference":"POPULARITY"
}' \
-H 'Content-Type: application/json' \
-H "X-Goog-Api-Key: API_KEY" \
-H "X-Goog-FieldMask: places.displayName,places.addressDescriptor" \
https://places.googleapis.com/v1/places:searchNearby

响应包含请求中指定的地点、附近地标及其与该地点的距离列表,以及区域及其与该地点的包含关系列表:

  {
    "places": [
      {
        "displayName": {
          "text": "Westfield Valley Fair",
          "languageCode": "en"
        },
        "addressDescriptor": {
          "landmarks": [
            {
              "name": "places/ChIJ62_oCR7Lj4AR_MGWkSPotD4",
              "placeId": "ChIJ62_oCR7Lj4AR_MGWkSPotD4",
              "displayName": {
                "text": "Nordstrom",
                "languageCode": "en"
              },
              "types": [
                "clothing_store",
                "department_store",
                "establishment",
                "point_of_interest",
                "shoe_store",
                "store"
              ],
              "straightLineDistanceMeters": 114.76984,
              "travelDistanceMeters": 114.261856
            },
            {
              "name": "places/ChIJgexMlR_Lj4ARiKCKuhNnjn0",
              "placeId": "ChIJgexMlR_Lj4ARiKCKuhNnjn0",
              "displayName": {
                "text": "Valley Fair Mall Eyexam of CA",
                "languageCode": "en"
              },
              "types": [
                "establishment",
                "health",
                "point_of_interest"
              ],
              "straightLineDistanceMeters": 131.62566,
              "travelDistanceMeters": 237.33253
            },
            {
              "name": "places/ChIJWWIlNx7Lj4ARpe1E0ob-_GI",
              "placeId": "ChIJWWIlNx7Lj4ARpe1E0ob-_GI",
              "displayName": {
                "text": "Din Tai Fung",
                "languageCode": "en"
              },
              "types": [
                "establishment",
                "food",
                "point_of_interest",
                "restaurant"
              ],
              "straightLineDistanceMeters": 110.0775,
              "travelDistanceMeters": 171.41951
            },
            {
              "name": "places/ChIJwyfPQx7Lj4AR7bYI2A2Yc54",
              "placeId": "ChIJwyfPQx7Lj4AR7bYI2A2Yc54",
              "displayName": {
                "text": "Abercrombie & Fitch",
                "languageCode": "en"
              },
              "types": [
                "clothing_store",
                "establishment",
                "point_of_interest",
                "shoe_store",
                "store"
              ],
              "spatialRelationship": "DOWN_THE_ROAD",
              "straightLineDistanceMeters": 53.620117,
              "travelDistanceMeters": 2.4578214
            },
            {
              "name": "places/ChIJpycNQx7Lj4ARjhXw3PrM_kU",
              "placeId": "ChIJpycNQx7Lj4ARjhXw3PrM_kU",
              "displayName": {
                "text": "Hollister Co.",
                "languageCode": "en"
              },
              "types": [
                "clothing_store",
                "establishment",
                "point_of_interest",
                "shoe_store",
                "store"
              ],
              "spatialRelationship": "DOWN_THE_ROAD",
              "straightLineDistanceMeters": 56.53726,
              "travelDistanceMeters": 15.418246
            }
          ],
          "areas": [
            {
              "name": "places/ChIJb3F-EB7Lj4ARnHApQ_Hu1gI",
              "placeId": "ChIJb3F-EB7Lj4ARnHApQ_Hu1gI",
              "displayName": {
                "text": "Westfield Valley Fair",
                "languageCode": "en"
              },
              "containment": "WITHIN"
            },
            {
              "name": "places/ChIJXYuykB_Lj4AR1Ot8nU5q26Q",
              "placeId": "ChIJXYuykB_Lj4AR1Ot8nU5q26Q",
              "displayName": {
                "text": "Valley Fair",
                "languageCode": "en"
              },
              "containment": "WITHIN"
            },
            {
              "name": "places/ChIJtYoUX2DLj4ARKoKOb1G0CpM",
              "placeId": "ChIJtYoUX2DLj4ARKoKOb1G0CpM",
              "displayName": {
                "text": "Central San Jose",
                "languageCode": "en"
              },
              "containment": "OUTSKIRTS"
            }
          ]
        }
      },
  /.../
  }

试试看!

借助 API Explorer,您可以发出示例请求,以便熟悉 API 和 API 选项。

  1. 选择页面右侧的 API 图标 api

  2. (可选)修改请求参数。

  3. 选择执行按钮。在对话框中,选择您要用于提出请求的账号。

  4. 在 APIs Explorer 面板中,选择全屏图标 fullscreen 以展开 APIs Explorer 窗口。