חישוב סיכום המסלול
כדי להשתמש בחיפוש טקסט (חדש) או בחיפוש בסביבה (חדש) כדי לחשב את משך הנסיעה והמרחק לכל מקום בתגובה:
-
מעבירים את הפרמטר
RoutingParametersבבקשה, ומשתמשים ב-setOrigin()כדי לציין את הקואורדינטות של קו הרוחב וקו האורך של נקודת המוצא של המסלול. הפרמטר הזה נדרש כדי לחשב את משך הזמן והמרחק לכל מקום בתגובה. -
כשיוצרים את אובייקט הבקשה, מוסיפים את
.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מכיל רק את השדות שמוגדרים ברשימת השדות שנשלחה בבקשה.
לא חייבים להשתמש באותן קואורדינטות להגבלת המיקום ולנקודת המוצא של הניתוב. לדוגמה, הגדרתם את הגבלת המיקומים לנקודת המרכז של סידני כדי להגביל את תוצאות החיפוש לעיגול הזה. אבל אז אתם מגדירים את נקודת המוצא של המסלול לקואורדינטות של הבית שלכם, כלומר למיקום אחר בתוך מעגל החיפוש. הבקשה מגבילה את תוצאות החיפוש למעגל, ומחשבת את סיכומי המסלולים על סמך מיקום הבית.
בחירת אפשרויות נסיעה
כברירת מחדל, חישובי משך הזמן והמרחק הם לרכב. עם זאת, אתם יכולים לשלוט בסוג כלי הרכב ובאפשרויות אחרות בחיפוש.
-
משתמשים ב-
routingParameters.setTravelMode()כדי להגדיר את אמצעי התחבורה ל-DRIVE, ל-BICYCLE, ל-WALKאו ל-TWO_WHEELER. מידע נוסף על האפשרויות האלה זמין במאמר סוגי כלי רכב שזמינים לחישוב מסלולים. -
משתמשים ב-
routingParameters.setRoutingPreference()כדי להגדיר את אפשרות העדפת הניתוב ל-TRAFFIC_UNAWARE(ברירת מחדל), ל-TRAFFIC_AWAREאו ל-TRAFFIC_AWARE_OPTIMAL. לכל אפשרות יש רמות שונות של איכות נתונים וזמן אחזור. מידע נוסף זמין במאמר איך מציינים אם לכלול נתוני תנועה ואיך. -
משתמשים ב-
routingParameters.setRouteModifiers()כדי לציין אתavoidTolls,avoidHighways,avoidFerriesו-avoidIndoor. מידע נוסף על האפשרויות האלה זמין במאמר ציון מאפייני מסלול שרוצים להימנע מהם.
בדוגמה הבאה, מציינים את אמצעי התחבורה כ-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(); });