חישוב סיכום הניתוב
כדי להשתמש בחיפוש טקסט (חדש) או בחיפוש בסביבה הקרובה (חדש) כדי לחשב את משך הנסיעה והמרחק לכל מקום בתשובה:
-
מעבירים את הפרמטר
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(); });