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

מפתחים באזור הכלכלי האירופי (EEA)

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

שירות מסלולים (גרסה קודמת) לעומת מחלקת מסלול

פרמטרים של בקשות

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

Directions Service (הגרסה הקודמת) Route
אמצעי תשלום אחד (DirectionsService.route()) אמצעי תשלום אחד (Route.computeRoutes())

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

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 (הגרסה הקודמת) Route
אמצעי תשלום אחד (route()) אמצעי תשלום אחד (computeRoutes())
אמצעי תשלום אחד (DirectionsRenderer.setDirections()) השיטה createPolylines(), השיטה createWaypointAdvancedMarkers()

השוואת קודים

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

בשירות המסלולים הקודם, האובייקט DirectionsRenderer משמש להצגת קווים פוליגוניים וסמנים שמייצגים את תוצאות המסלולים במפה. בספריית המסלולים, האובייקט 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, ואז משתמש ב-method‏ createPolylines כדי לשרטט קו פוליגוני במפה, וב-method‏ 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;
});