محاسبه خلاصه مسیریابی

برای استفاده از جستجوی نوشتاری (جدید) یا جستجوی اطراف (جدید) برای محاسبه مدت و مسافت سفر به هر مکان در پاسخ:

  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 فقط حاوی فیلدهایی است که توسط فهرست فیلد ارسال‌شده در درخواست تعریف شده است.

لازم نیست از مختصات یکسانی برای محدودیت مکان و برای مبدأ مسیریابی استفاده کنید. برای مثال، محدودیت مکان‌ها را روی نقطه مرکزی سیدنی تنظیم می‌کنید تا نتایج جستجو را به آن دایره محدود کنید. اما سپس مبدأ مسیریابی را روی مختصات خانه‌تان تنظیم می‌کنید، یعنی روی مکان دیگری در دایره جستجو. سپس درخواست نتایج جستجو را به این دایره محدود می‌کند و خلاصه مسیر را براساس مکان خانه‌تان محاسبه می‌کند.

مشخص کردن گزینه‌های سفر

به‌طور پیش‌فرض، محاسبات مدت و فاصله برای خودرو است. بااین‌حال، می‌توانید نوع وسیله نقلیه و همچنین گزینه‌های دیگر را در جستجو کنترل کنید.

در مثال بعدی، حالت سفر را به‌عنوان 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();
    });