计算路线摘要

如需使用 文本搜索(新) 或 附近搜索(新) 来计算响应中每个地点的行程时长和距离,请执行以下操作:

  1. 在请求中传递 RoutingParameters 参数,并使用 setOrigin() 指定路线起点的纬度和经度坐标。此参数是计算响应中每个地点的时长和距离所必需的参数。

  2. 构建请求对象时,添加 .setRoutingSummariesIncluded(true)。

在以下请求中,您可以计算文本搜索(新)响应中每个 地点的行程时长和距离:

// Specify the list of fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Define the routing parameters object and pass the routing origin.
RoutingParameters routingParameters = RoutingParameters.builder()
    .setOrigin(toLatLng("-33.8688, 151.1957362"))
    .build();

// Use the builder to create a SearchByTextRequest object and pass the routing parameters.
// Set setRoutingSummariesIncluded to true.
final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food in Sydney, Australia", placeFields)
    .setMaxResultCount(10)
    .setRoutingParameters(routingParameters)
    .setRoutingSummariesIncluded(true)
    .build();

// Call PlacesClient.searchByText() to perform the search.
// Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg objects.
placesClient.searchByText(searchByTextRequest)   
    .addOnSuccessListener(response -> {
      List<Place> places = response.getPlaces();
      List<RoutingSummary> routingSummaries = response.getRoutingSummaries();
      List<Leg> legs = routingSummaries.get(0).getLegs();
      Duration duration = legs.get(0).getDuration();
    });

The SearchByTextResponse 类表示搜索请求的响应。 您可以调用 SearchByTextResponse.getRoutingSummaries() 来返回路线摘要列表。 SearchByTextResponse 对象还包含:

  • 一个 Place 对象列表,其中每个 Place 对象代表一个匹配的地点。
  • 每个 Place 对象仅包含请求中传递的字段列表定义的字段。

在此示例中,您可以计算附近搜索响应中每个地点的行程时长和距离。此示例搜索的是澳大利亚悉尼的餐厅,并将地点限制条件和路线起点设置为相同的纬度和经度坐标:

// Specify the list of fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Define the search area as a 500 meter diameter circle in Sydney, Australia.
LatLng center = new LatLng(-33.8688, 151.1957362);
CircularBounds circle = CircularBounds.newInstance(center, /* radius = */ 500);

// Define the routing parameters object and pass the routing origin.
RoutingParameters routingParameters = RoutingParameters.builder()
    .setOrigin(toLatLng("-33.8688, 151.1957362"))
    .build();

// Use the builder to create a SearchNearbyRequest object and pass the routing parameters.
// Set setRoutingSummariesIncluded to true.
final SearchNearbyRequest searchNearbyRequest =
SearchNearbyRequest.builder(/* location restriction = */ circle, placeFields)
    .setIncludedTypes(includedTypes)
    .setMaxResultCount(10)
    .setRoutingParameters(routingParameters)
    .setRoutingSummariesIncluded(true)
    .build();

// Call PlacesClient.searchNearby() to perform the search.
// Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg objects.
placesClient.searchNearby(searchNearbyRequest)
    .addOnSuccessListener(response -> {
      List<Place> places = response.getPlaces();
      List<RoutingSummary> routingSummaries = response.getRoutingSummaries();
      List<Leg> legs = routingSummaries.get(0).getLegs();
      Duration duration = legs.get(0).getDuration();
    });

The SearchNearbyResponse 类表示搜索请求的响应。 您可以调用 SearchNearbyResponse.getRoutingSummaries() 来返回路线摘要列表。 SearchNearbyResponse 对象还包含:

  • 一个 Place 对象列表,其中每个 Place 对象代表一个匹配的地点。
  • 每个 Place 对象仅包含请求中传递的字段列表 定义的字段。

您不必为地点限制条件和 路线起点使用相同的坐标。例如,您可以将地点限制条件设置为悉尼的中心 点,以将搜索结果限制在该圆圈内。但随后,您将路线起点设置为您家的坐标,这意味着搜索圆圈内的另一个地点。然后,请求会将搜索结果限制在该 圆圈内,并根据您家的 位置计算路线摘要。

指定行程选项

默认情况下,时长和距离计算是针对汽车的。不过,您可以在搜索中控制车辆类型以及其他选项。

  • 使用 routingParameters.setTravelMode() 将交通方式设置为 DRIVE、BICYCLE、WALK 或 TWO_WHEELER。如需详细了解这些选项,请参阅路线的可用车辆类型。

  • 使用 routingParameters.setRoutingPreference() 将路线 偏好设置选项设置为 TRAFFIC_UNAWARE(默认)、TRAFFIC_AWARE 或 TRAFFIC_AWARE_OPTIMAL。每个选项的数据质量和 延迟时间各不相同。如需了解详情,请参阅指定是否以及如何包含路况 数据。
  • 使用 routingParameters.setRouteModifiers() 指定 avoidTolls、avoidHighways、avoidFerries 和 avoidIndoor。如需详细了解这些选项,请参阅指定要避开的路线特征。

在下一个示例中,您可以将交通方式指定为 DRIVE,并避开高速公路:

// Specify the list of fields to return.
final List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME);

// Define the routing modifiers object.
RouteModifiers routeModifiers = RouteModifiers.builder()
    .setAvoidHighways(true)
    .build();

// Define the routing parameters object and pass the routing origin.
RoutingParameters routingParameters = RoutingParameters.builder()
    .setOrigin(toLatLng("-33.8688, 151.1957362"))
    .setTravelMode(DRIVE)
    .setRouteModifiers(routeModifiers)
    .build();

// Use the builder to create a SearchByTextRequest object and pass the routing parameters.
// Set setRoutingSummariesIncluded to true.
final SearchByTextRequest searchByTextRequest = SearchByTextRequest.builder("Spicy Vegetarian Food in Sydney, Australia", placeFields)
    .setMaxResultCount(10)
    .setRoutingParameters(routingParameters)
    .setRoutingSummariesIncluded(true)
    .build();

// Call PlacesClient.searchByText() to perform the search.
// Define a response handler to process the returned Lists of Place objects, RoutingSummary objects, and Leg objects.
placesClient.searchByText(searchByTextRequest)   
    .addOnSuccessListener(response -> {
      List<Place> places = response.getPlaces();
      List<RoutingSummary> routingSummaries = result.getRoutingSummaries();
      List<Leg> legs = routingSummaries.get(0).getLegs();
      Duration duration = legs.get(0).getDuration();
    });