Tính toán thông tin tóm tắt về việc định tuyến

Cách sử dụng Tìm kiếm bằng văn bản (Mới) hoặc Tìm kiếm lân cận (Mới) để tính toán thời gian và khoảng cách di chuyển đến từng địa điểm trong câu trả lời:

  1. Truyền tham số RoutingParameters trong yêu cầu, sử dụng setOrigin() để chỉ định toạ độ vĩ độ và kinh độ của điểm bắt đầu định tuyến. Tham số này là bắt buộc để tính toán thời lượng và khoảng cách đến từng địa điểm trong phản hồi.

  2. Khi tạo đối tượng yêu cầu, hãy thêm .setRoutingSummariesIncluded(true).

Trong yêu cầu sau, bạn sẽ tính toán thời gian di chuyển và khoảng cách đến từng địa điểm trong phản hồi của tính năng Tìm kiếm bằng văn bản (mới):

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

Lớp SearchByTextResponse đại diện cho phản hồi từ một yêu cầu tìm kiếm. Bạn có thể gọi SearchByTextResponse.getRoutingSummaries() để trả về danh sách thông tin tóm tắt về việc định tuyến. Đối tượng SearchByTextResponse cũng chứa:

  • Một danh sách các đối tượng Place đại diện cho tất cả những địa điểm trùng khớp, trong đó mỗi địa điểm trùng khớp có một đối tượng Place.
  • Mỗi đối tượng Place chỉ chứa các trường do danh sách trường được truyền trong yêu cầu xác định.

Trong ví dụ này, bạn sẽ tính toán thời gian và khoảng cách di chuyển đến từng địa điểm trong phản hồi Tìm kiếm lân cận. Ví dụ này tìm kiếm các nhà hàng ở Sydney, Úc và đặt chế độ hạn chế về vị trí cũng như điểm xuất phát của tuyến đường thành cùng một toạ độ vĩ độ và kinh độ:

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

Lớp SearchNearbyResponse đại diện cho phản hồi từ một yêu cầu tìm kiếm. Bạn có thể gọi SearchNearbyResponse.getRoutingSummaries() để trả về danh sách thông tin tóm tắt về việc định tuyến. Đối tượng SearchNearbyResponse cũng chứa:

  • Một danh sách các đối tượng Place đại diện cho tất cả những địa điểm trùng khớp, với một đối tượng Place cho mỗi địa điểm trùng khớp.
  • Mỗi đối tượng Place chỉ chứa các trường do danh sách trường xác định được truyền trong yêu cầu.

Bạn không cần phải sử dụng cùng một toạ độ cho hạn chế về vị trí và cho điểm bắt đầu định tuyến. Ví dụ: bạn đặt chế độ hạn chế về vị trí thành điểm trung tâm của Sydney để hạn chế kết quả tìm kiếm trong vòng tròn đó. Nhưng sau đó, bạn đặt điểm bắt đầu chỉ đường thành toạ độ nhà mình, tức là một vị trí khác trong vòng tròn tìm kiếm. Sau đó, yêu cầu này sẽ giới hạn kết quả tìm kiếm trong vòng tròn và tính toán thông tin tóm tắt về đường đi dựa trên vị trí nhà của bạn.

Chỉ định các lựa chọn di chuyển

Theo mặc định, các phép tính về thời lượng và khoảng cách là dành cho ô tô. Tuy nhiên, bạn có thể kiểm soát loại xe cũng như các lựa chọn khác trong quá trình tìm kiếm.

Trong ví dụ tiếp theo, bạn chỉ định chế độ di chuyển là DRIVE và tránh đường cao tốc:

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