Route क्लास पर माइग्रेट करना

यूरोपियन इकनॉमिक एरिया (ईईए) के डेवलपर

Maps JavaScript API की नई Routes लाइब्रेरी में Route क्लास शामिल है. यह रास्ता बताने वाली लेगसी सेवा की जगह इस्तेमाल की जाती है. इस पेज पर, पुरानी Directions Service और नई Route क्लास के बीच के अंतर के बारे में बताया गया है. साथ ही, तुलना करने के लिए कुछ कोड भी दिए गए हैं.

Directions service (लेगसी) बनाम Route क्लास

अनुरोध के पैरामीटर

इस टेबल में, लेगसी Directions service और 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

अलग-अलग तरीकों की तुलना

यहां दी गई टेबल में, लेगसी Directions सेवा और Route क्लास के मुख्य तरीकों की तुलना की गई है.

Directions Service (लेगसी) Route
route() तरीका computeRoutes() तरीका
DirectionsRenderer.setDirections() तरीका createPolylines() तरीका, createWaypointAdvancedMarkers() तरीका

कोड की तुलना करना

इस सेक्शन में, कोड के दो मिलते-जुलते हिस्सों की तुलना करके, लेगसी Directions सेवा और नई Route क्लास के बीच के अंतर को समझाया गया है. कोड स्निपेट में, हर एपीआई पर ज़रूरी कोड दिखाया गया है. इससे, दिशा-निर्देशों का अनुरोध किया जा सकता है. इसके बाद, नतीजे का इस्तेमाल करके मैप पर पॉलीलाइन और मार्कर बनाए जा सकते हैं.

लेगसी Directions service में, DirectionsRenderer ऑब्जेक्ट का इस्तेमाल, मैप पर दिशा-निर्देशों के नतीजों को दिखाने के लिए, पॉलीलाइन और मार्कर दिखाने के लिए किया जाता है. Routes लाइब्रेरी में, DirectionsRenderer ऑब्जेक्ट को createPolylines() और createWaypointAdvancedMarkers() तरीकों से बदल दिया गया है. इस पेज पर, पुरानी Directions Service और नई Route क्लास के बीच के अंतर के बारे में बताया गया है. साथ ही, तुलना करने के लिए कुछ कोड भी दिए गए हैं.

ड्राइविंग के दिशा निर्देश पाएं

Directions service (लेगसी)

यहां दिया गया कोड, लेगसी Directions service का इस्तेमाल करके, ड्राइविंग के लिए रास्ते के निर्देश पाता है. इसके बाद, मैप पर पॉलीलाइन और मार्कर बनाने के लिए, 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;
});