रास्ते की जानकारी का आकलन करना

जवाब में मौजूद हर जगह तक पहुंचने में लगने वाला समय और दूरी का हिसाब लगाने के लिए, टेक्स्ट सर्च (नई) या आस-पास की जगहें ढूंढने की सुविधा (नई) का इस्तेमाल करें:

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