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