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

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

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