הספרייה החדשה Routes Library, Maps JavaScript API כוללת את המחלקה Route, שמחליפה את הגרסה הקודמת של שירות המסלולים. בדף הזה מוסברים ההבדלים בין שירות הניווט מהדור הקודם לבין המחלקה החדשה Route, ומוצג קוד להשוואה.
שירות הכוונה (גרסה קודמת) לעומת מחלקת מסלולים
פרמטרים של בקשה
בטבלה הבאה מוצגת השוואה בין פרמטרי הבקשה של שירות ניתוב הדרכים מדור קודם לבין המחלקה Route.
השוואה בין שיטות
בטבלה הבאה מוצגת השוואה בין שיטות מרכזיות בשירות הישן של מסלולי נסיעה לבין המחלקה Route.
| Directions Service (Legacy) | Route |
|---|---|
route() method |
computeRoutes() method |
DirectionsRenderer.setDirections() method |
createPolylines() method,
createWaypointAdvancedMarkers() method
|
השוואת קוד
בקטע הזה מוצגות שתי דוגמאות דומות של קוד כדי להמחיש את ההבדלים בין שירות הניתוב מהדור הקודם לבין המחלקה החדשה Route. קטעי הקוד מציגים את הקוד שנדרש בכל API כדי לשלוח בקשה לקבלת מסלול, ואז להשתמש בתוצאה כדי לצייר קו שבור וסמנים במפה.
בשירות הישן של מסלולי נסיעה, נעשה שימוש באובייקט
DirectionsRenderer
כדי להציג קווים שבורים וסמנים שמייצגים את תוצאות מסלולי הנסיעה במפה. בספריית Routes, האובייקט DirectionsRenderer הוחלף ב-methods createPolylines() ו-createWaypointAdvancedMarkers(). בדף הזה מוסברים ההבדלים בין Directions Service מהדור הקודם לבין המחלקה החדשה Route, ומוצג קוד להשוואה.
קבל מסלול נסיעה
שירות הכוונה (גרסה קודמת)
הקוד הבא מקבל הוראות נסיעה באמצעות שירות ההוראות מדור קודם, ואז משתמש ב-DirectionsRenderer כדי לצייר קו פוליגוני וסמנים במפה:
// Define a simple request. var request = { origin: 'Mountain View, CA', destination: 'San Francisco, CA', travelMode: 'DRIVING' }; // Call the Directions Service to get the directions. directionsService.route(request, function(result, status) { if (status == 'OK') { directionsRenderer.setDirections(result); // Add polyline and markers to the map. } });
סיווג המסלול
הקוד הבא מקבל מסלול נסיעה באמצעות המחלקה החדשה Route, ואז משתמש בשיטה createPolylines כדי לשרטט קו פוליגוני במפה, ובשיטה createWaypointAdvancedMarkers כדי לשרטט סמנים במפה.
הסמנים לא מוצגים אוטומטית בכיתה החדשה Route. כדי להציג סמנים, צריך להפעיל את הפונקציה createWaypointAdvancedMarkers.
TypeScript
// Define a routes request. const request: google.maps.routes.ComputeRoutesRequest = { origin: 'Mountain View, CA', destination: 'San Francisco, CA', travelMode: 'DRIVING', fields: ['path'], // Request fields needed to draw polylines. }; // Call computeRoutes to get the directions. const { routes } = await Route.computeRoutes(request); // Use createPolylines to create polylines for the route. if (!routes) { console.warn('No routes found.'); return; } mapPolylines = routes[0].createPolylines(); // Add polylines to the map. mapPolylines.forEach((polyline) => { polyline.setMap(map); }); // Create markers to start and end points. const markers = await routes[0].createWaypointAdvancedMarkers(); // Add markers to the map markers.forEach((marker) => { marker.map = map; });
JavaScript
// Define a routes request. const request = { origin: 'Mountain View, CA', destination: 'San Francisco, CA', travelMode: 'DRIVING', fields: ['path'], // Request fields needed to draw polylines. }; // Call computeRoutes to get the directions. const { routes } = await Route.computeRoutes(request); // Use createPolylines to create polylines for the route. if (!routes) { console.warn('No routes found.'); return; } mapPolylines = routes[0].createPolylines(); // Add polylines to the map. mapPolylines.forEach((polyline) => { polyline.setMap(map); }); // Create markers to start and end points. const markers = await routes[0].createWaypointAdvancedMarkers(); // Add markers to the map markers.forEach((marker) => { marker.map = map; });