নতুন রেন্ডারিং পদ্ধতিতে মাইগ্রেট করুন

এই নির্দেশিকা আপনাকে দেখায় কিভাবে রুট ক্লাসের জন্য নতুন রেন্ডারিং পদ্ধতিতে স্থানান্তর করতে হয়। দিকনির্দেশ পরিষেবাতে (উত্তরাধিকার), রেন্ডারিং পদ্ধতিগুলি DirectionsRenderer শ্রেণীর অংশ ছিল। রুট ক্লাস (বিটা) দুটি নতুন রেন্ডারিং পদ্ধতি প্রদান করে: createPolylines এবং createWaypointAdvancedMarkers

লিগ্যাসি DirectionsRenderer

দিকনির্দেশ পরিষেবাতে (উত্তরাধিকার), রেন্ডারিং পদ্ধতিগুলি DirectionsRenderer শ্রেণীর অংশ ছিল। DirectionsRenderer ক্লাস পলিলাইনের প্রদর্শন পরিচালনা করে, যেকোনো সংশ্লিষ্ট মার্কার, সেইসাথে ধাপের পাঠ্য প্রদর্শন; এটির নিম্নলিখিত পদ্ধতি রয়েছে:

  • setDirections() - প্রদত্ত দিকনির্দেশের প্রতিক্রিয়া রেন্ডার করে।
  • setMap() - মানচিত্র সেট করে যার উপর নির্দেশের প্রতিক্রিয়া রেন্ডার করতে হবে।
  • setPanel() - একটি প্যানেলে পাঠ্য পদক্ষেপের একটি সিরিজ হিসাবে দিকনির্দেশ প্রদর্শন করে।

নিচের উদাহরণটি দেখায় কিভাবে একটি মানচিত্রে দিকনির্দেশ রেন্ডার করতে DirectionsRenderer ক্লাস ব্যবহার করতে হয়।

function initMap() {
  var directionsService = new google.maps.DirectionsService();
  var directionsRenderer = new google.maps.DirectionsRenderer();
  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
    zoom:7,
    center: chicago
  }
  var map = new google.maps.Map(document.getElementById('map'), mapOptions);
  // Set the map on the directions renderer.
  directionsRenderer.setMap(map);
  // Set the panel to display the directions as a series of textual steps.
  directionsRenderer.setPanel(document.getElementById('directionsPanel'));
}

function calcRoute() {
  var start = document.getElementById('start').value;
  var end = document.getElementById('end').value;
  var request = {
    origin: start,
    destination: end,
    travelMode: 'DRIVING'
  };
  // Call the directions service to get the directions.
  directionsService.route(request, function(response, status) {
    if (status == 'OK') {
      // Render the polyline and markers on the map.
      directionsRenderer.setDirections(response);
    }
  });
}
    

Route ক্লাস (বিটা)

রুট ক্লাস (বিটা) নিম্নলিখিত নতুন রেন্ডারিং পদ্ধতিগুলি প্রদান করে, যা লিগ্যাসি DirectionsRenderer ক্লাস পদ্ধতিগুলিকে প্রতিস্থাপন করে:

  • createPolylines
  • createWaypointAdvancedMarkers

Legacy DirectionsRenderer ক্লাসে Route ক্লাস setPanel() পদ্ধতির সমতুল্য নেই। পাঠ্য পদক্ষেপগুলি প্রদর্শন করতে, আপনাকে অবশ্যই এইচটিএমএল উপাদানগুলি ম্যানুয়ালি তৈরি করতে হবে এবং সেগুলিকে DOM-এ সন্নিবেশ করতে হবে৷ নিম্নলিখিত উদাহরণটি দেখায় যে কিভাবে রুট ক্লাস ব্যবহার করে একটি মানচিত্রে দিকনির্দেশ রেন্ডার করতে হয় এবং পাঠ্য পদক্ষেপগুলি প্রদর্শন করতে ম্যানুয়ালি HTML উপাদানগুলি তৈরি করে৷

let map;
let mapPolylines = [];
let markers = [];
let center = { lat: 37.447646, lng: -122.113878 }; // Palo Alto, CA

// Initialize and add the map.
async function initMap() {
  // Request the needed libraries.
  const { Map } = await google.maps.importLibrary('maps') as google.maps.MapsLibrary;
  const { Route } = await google.maps.importLibrary('routes') as google.maps.Routes;

  map = new Map(document.getElementById("map"), {
    zoom: 12,
    center,
    mapTypeControl: false,
    mapId: 'DEMO_MAP_ID',
  });

  // Define a simple directions request.
  const request = {
    origin: 'Mountain View, CA',
    destination: 'San Francisco, CA',
    travelMode: 'DRIVING',
    fields: ['legs'],
  };

  // Call computeRoutes to get the directions.
  const { routes } = await Route.computeRoutes(request);
  
  // Use createPolylines to create polylines for the route.
  mapPolylines = routes[0].createPolylines();
  // Add polylines to the map.
  mapPolylines.forEach((polyline) => polyline.setMap(map));
  
  fitMapToPath(routes[0].path);

  // Add markers to start and end points.
  const markers = await routes[0].createWaypointAdvancedMarkers({map});
  

  // Render navigation instructions.
  const directionsPanel = document.getElementById("directions-panel");

  if (!routes || routes.length === 0) {
    if (directionsPanel) {
      directionsPanel.textContent = "No routes available.";
    }
  }

  const route = routes[0];
  if (!route.legs || route.legs.length === 0) {
    if (directionsPanel) {
      directionsPanel.textContent = "The route has no legs.";
    }
    return;
  }

  const fragment = document.createDocumentFragment();

  route.legs.forEach((leg, index) => {
    const legContainer = document.createElement("div");
    legContainer.className = "directions-leg";
    legContainer.setAttribute("aria-label", `Leg ${index + 1}`);

    // Leg Title
    const legTitleElement = document.createElement("h3");
    legTitleElement.textContent = `Leg ${index + 1} of ${route.legs.length}`;
    legContainer.appendChild(legTitleElement);

    if (leg.steps && leg.steps.length > 0) {
      // Add steps to an ordered list
      const stepsList = document.createElement("ol");
      stepsList.className = "directions-steps";

      leg.steps.forEach((step, stepIndex) => {
        const stepItem = document.createElement("li");
        stepItem.className = "direction-step";
        stepItem.setAttribute("aria-label", `Step ${stepIndex + 1}`);

        // Maneuver
        if (step.maneuver) {
          const maneuverNode = document.createElement("p");
          maneuverNode.textContent = step.maneuver;
          maneuverNode.className = "maneuver";
          stepItem.appendChild(maneuverNode);
        }

        // Distance and Duration
        if (step.localizedValues) {
          const distanceNode = document.createElement("p");
          distanceNode.textContent = `${step.localizedValues.distance} (${step.localizedValues.staticDuration})`;
          distanceNode.className = "distance";
          stepItem.appendChild(distanceNode);
        }

        // Instructions
        if (step.instructions) {
          const instructionsNode = document.createElement("p");
          instructionsNode.textContent = step.instructions;
          instructionsNode.className = "instruction";
          stepItem.appendChild(instructionsNode);
        }

        stepsList.appendChild(stepItem);
      });
      legContainer.appendChild(stepsList);
    }

    fragment.appendChild(legContainer);
    directionsPanel?.appendChild(fragment);
  });
  
}

// Helper function to fit the map to the path.
async function fitMapToPath(path) {
  const { LatLngBounds } = await google.maps.importLibrary('core') as google.maps.CoreLibrary;
  const bounds = new LatLngBounds();
  path.forEach((point) => {
    bounds.extend(point);
  });
  map.fitBounds(bounds);
}

initMap();