חישוב סיכום הניתוב

כדי להשתמש בחיפוש טקסט (חדש) או בחיפוש בסביבה (חדש) כדי לחשב את משך הנסיעה והמרחק לכל מקום בתשובה:

  1. מעבירים את הפרמטר RoutingParameters בבקשה, באמצעות setOrigin() כדי לציין את קואורדינטות קו האורך וקו הרוחב של נקודת המוצא של הניתוב. הפרמטר הזה נדרש כדי לחשב את משך הזמן והמרחק לכל מקום בתגובה.

  2. כשיוצרים את אובייקט הבקשה, מוסיפים את .setRoutingSummariesIncluded(true).

בדוגמה הבאה, מחשבים את משך הנסיעה ואת המרחק לכל מקום בתגובה של Text Search (חדש):

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