计算路由摘要

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

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

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

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

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

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

指定出行方式

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

  • 使用 routingParameters.setTravelMode() 将传输模式设置为 DRIVEBICYCLEWALKTWO_WHEELER。如需详细了解这些选项,请参阅路线的可用车辆类型

  • 使用 routingParameters.setRoutingPreference() 将路由偏好设置选项设为 TRAFFIC_UNAWARE(默认)、TRAFFIC_AWARETRAFFIC_AWARE_OPTIMAL。每种选项的数据质量和延迟时间各不相同。如需了解详情,请参阅指定是否要包含流量数据以及如何包含
  • 使用 routingParameters.setRouteModifiers() 指定 avoidTollsavoidHighwaysavoidFerriesavoidIndoor。如需详细了解这些选项,请参阅指定要避开的路线地图项

在下一个示例中,您将旅行模式指定为 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();
    });