העברה אל מחלקת Route

ספריית המסלולים החדשה, Maps JavaScript API, כוללת את המחלקה Route, שמחליפה את שירות המסלולים מדור קודם. בדף הזה מוסבר על ההבדלים בין שירות הניווט מהדור הקודם לבין המחלקה החדשה Route, ומוצג קוד להשוואה.

ההבדלים בין שירות הכיוונים (גרסה קודמת) לבין מחלקת המסלולים (בטא)

פרמטרים של בקשה

בטבלה הבאה מוצגת השוואה בין פרמטרי הבקשה של שירות הניווט הקודם לבין המחלקה Route.

Directions Service (Legacy) Route (בטא)
DirectionsService.route() method Route.computeRoutes() method

פרמטרים נדרשים

origin origin
destination destination
travelMode travelMode (אופציונלי)

פרמטרים אופציונליים

optimizeWaypoints optimizeWaypointOrder
provideRouteAlternatives computeAlternativeRoutes
avoidFerries, avoidHighways, avoidTolls routeModifiers
drivingOptions departureTime, trafficModel
region region
transitOptions transitPreference
arrivalTime arrivalTime
unitSystem units
waypoints intermediates

השוואה בין שיטות

בטבלה הבאה מוצגת השוואה בין שיטות מרכזיות בשירות הישן של מסלולי נסיעה לבין המחלקה Route.

Directions Service (Legacy) Route (בטא)
route() method computeRoutes() method
DirectionsRenderer.setDirections() method createPolylines() method, createWaypointAdvancedMarkers() method

השוואת קוד

בקטע הזה מוצגות שתי דוגמאות דומות של קוד כדי להמחיש את ההבדלים בין שירות ה-Directions מהדור הקודם לבין המחלקה החדשה Route. קטעי הקוד מציגים את הקוד שנדרש בכל API כדי לשלוח בקשה לקבלת מסלול, ואז להשתמש בתוצאה כדי לצייר קו שבור וסמנים במפה.

בשירות הישן של מסלולי נסיעה, נעשה שימוש באובייקט DirectionsRenderer כדי להציג קווים שבורים וסמנים שמייצגים את תוצאות מסלולי הנסיעה במפה. בספריית המסלולים, האובייקט DirectionsRenderer הוחלף בשיטות 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.

// Define a simple request.
const request = {
  origin: 'Mountain View, CA',
  destination: 'San Francisco, CA',
  travelMode: 'DRIVING',
  fields: ['path', 'legs'], // Request fields needed to draw polylines.
};

// Call computeRoutes to get the directions.
const {routes} = await Route.computeRoutes(request);

// Use createPolylines to create a polyline for the route.
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.setMap(map));