احتساب ملخّص التوجيه

لاستخدام البحث النصي (الجديد) أو بحث في الجوار (الجديد) لحساب مدة الرحلة والمسافة إلى كل مكان في الردّ:

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