রুট ম্যাট্রিক্স পাওয়া

ইউরোপীয় অর্থনৈতিক অঞ্চল (EEA)-এর ডেভেলপার

রুট ম্যাট্রিক্স হল রুট সংক্রান্ত তথ্যের একটি দ্বি-মাত্রিক অ্যারে, যেখানে সারিগুলি উৎস এবং কলামগুলি গন্তব্যের সাথে সম্পর্কিত। যাত্রা শুরুর স্থান ও গন্তব্যের তালিকা দেওয়া থাকলে, রুট ম্যাট্রিক্স ক্লাস প্রতিটি যাত্রা শুরুর স্থান থেকে শুরু করে প্রতিটি গন্তব্যে শেষ হওয়া রুটের দূরত্ব ও সময়কাল গণনা করে। একাধিক উৎস ও গন্তব্যের জন্য কোনও রুটের দূরত্ব ও সময় হিসেব করতে Route Matrix ক্লাস ব্যবহার করুন।

সম্পূর্ণ উদাহরণ সোর্স কোড দেখুন

এই উদাহরণে দেখানো হয়েছে যে একাধিক উৎস ও গন্তব্যের মধ্যে ভ্রমণের দূরত্ব ও সময় হিসেব করতে কীভাবে রুট ম্যাট্রিক্স ক্লাস ব্যবহার করতে হয়।

TypeScript

// Initialize and add the map.
let map: google.maps.Map;
const markers: google.maps.marker.AdvancedMarkerElement[] = [];
const center = { lat: 51.55, lng: -1.8 };

async function init(): Promise<void> {
    //  Request the needed libraries.
    const [
        { Map },
        { Place },
        { AdvancedMarkerElement, PinElement },
        { RouteMatrix },
        { LatLngBounds, UnitSystem },
    ] = await Promise.all([
        google.maps.importLibrary('maps'),
        google.maps.importLibrary('places'),
        google.maps.importLibrary('marker'),
        google.maps.importLibrary('routes'),
        google.maps.importLibrary('core'),
    ]);

    const bounds = new LatLngBounds();

    map = new Map(document.getElementById('map')!, {
        zoom: 8,
        center,
        mapId: 'DEMO_MAP_ID',
    });

    // Build the request using Place instances.
    const origin1 = new Place({
        id: 'ChIJ83WZp86p2EcRbMrkYqGncBQ', // Greenwich, London, UK
    });
    const origin2 = new Place({
        id: 'ChIJCSkVvleJc0gR8HHaTGpajKc', // Southampton, UK
    });
    const destinationA = new Place({
        id: 'ChIJYdizgWaDcUgRH9eaSy6y5I4', // Bristol, UK
    });
    const destinationB = new Place({
        id: 'ChIJ9VPsNNQCbkgRDmeGZdsGNBQ', // Cardiff, UK
    });

    await Promise.all([
        origin1.fetchFields({ fields: ['location', 'displayName'] }),
        origin2.fetchFields({ fields: ['location', 'displayName'] }),
        destinationA.fetchFields({ fields: ['location', 'displayName'] }),
        destinationB.fetchFields({ fields: ['location', 'displayName'] }),
    ]);

    const destinations = [destinationA, destinationB];
    const origins = [origin1, origin2];
    const request: google.maps.routes.ComputeRouteMatrixRequest = {
        origins,
        destinations,
        travelMode: 'DRIVING',
        units: UnitSystem.METRIC,
        fields: ['distanceMeters', 'durationMillis', 'condition'],
    };

    // Show the request.
    document.getElementById('request')!.innerText = JSON.stringify(
        request,
        null,
        2
    );

    // Get the RouteMatrix response.
    const response = await RouteMatrix.computeRouteMatrix(request);

    // Show the response.
    document.getElementById('response')!.innerText = JSON.stringify(
        response,
        null,
        2
    );

    // Add markers for the origins.
    for (const origin of origins) {
        if (origin.location) {
            const pin = new PinElement({
                glyphText: 'O',
                glyphColor: 'white',
                background: '#137333',
                borderColor: 'white',
            });
            const marker = new AdvancedMarkerElement({
                map,
                position: origin.location,
                title: `Origin: ${origin.displayName}`,
            });
            marker.append(pin);
            markers.push(marker);
            bounds.extend(origin.location);
        }
    }

    // Add markers for the destinations.
    for (const destination of destinations) {
        if (destination.location) {
            const pin = new PinElement({
                glyphText: 'D',
                glyphColor: 'white',
                background: '#C5221F',
                borderColor: 'white',
            });

            const marker = new AdvancedMarkerElement({
                map,
                position: destination.location,
                content: pin,
                title: `Destination: ${destination.displayName ?? 'Unknown'}`,
            });

            markers.push(marker);
            bounds.extend(destination.location);
        }
    }

    // Fit the map to the bounds of all markers.
    map.fitBounds(bounds);
}

void init();

জাভাস্ক্রিপ্ট

// Initialize and add the map.
let map;
const markers = [];
const center = { lat: 51.55, lng: -1.8 };

async function init() {
    //  Request the needed libraries.
    const [
        { Map },
        { Place },
        { AdvancedMarkerElement, PinElement },
        { RouteMatrix },
        { LatLngBounds, UnitSystem },
    ] = await Promise.all([
        google.maps.importLibrary('maps'),
        google.maps.importLibrary('places'),
        google.maps.importLibrary('marker'),
        google.maps.importLibrary('routes'),
        google.maps.importLibrary('core'),
    ]);

    const bounds = new LatLngBounds();

    map = new Map(document.getElementById('map'), {
        zoom: 8,
        center,
        mapId: 'DEMO_MAP_ID',
    });

    // Build the request using Place instances.
    const origin1 = new Place({
        id: 'ChIJ83WZp86p2EcRbMrkYqGncBQ', // Greenwich, London, UK
    });
    const origin2 = new Place({
        id: 'ChIJCSkVvleJc0gR8HHaTGpajKc', // Southampton, UK
    });
    const destinationA = new Place({
        id: 'ChIJYdizgWaDcUgRH9eaSy6y5I4', // Bristol, UK
    });
    const destinationB = new Place({
        id: 'ChIJ9VPsNNQCbkgRDmeGZdsGNBQ', // Cardiff, UK
    });

    await Promise.all([
        origin1.fetchFields({ fields: ['location', 'displayName'] }),
        origin2.fetchFields({ fields: ['location', 'displayName'] }),
        destinationA.fetchFields({ fields: ['location', 'displayName'] }),
        destinationB.fetchFields({ fields: ['location', 'displayName'] }),
    ]);

    const destinations = [destinationA, destinationB];
    const origins = [origin1, origin2];
    const request = {
        origins,
        destinations,
        travelMode: 'DRIVING',
        units: UnitSystem.METRIC,
        fields: ['distanceMeters', 'durationMillis', 'condition'],
    };

    // Show the request.
    document.getElementById('request').innerText = JSON.stringify(
        request,
        null,
        2
    );

    // Get the RouteMatrix response.
    const response = await RouteMatrix.computeRouteMatrix(request);

    // Show the response.
    document.getElementById('response').innerText = JSON.stringify(
        response,
        null,
        2
    );

    // Add markers for the origins.
    for (const origin of origins) {
        if (origin.location) {
            const pin = new PinElement({
                glyphText: 'O',
                glyphColor: 'white',
                background: '#137333',
                borderColor: 'white',
            });
            const marker = new AdvancedMarkerElement({
                map,
                position: origin.location,
                title: `Origin: ${origin.displayName}`,
            });
            marker.append(pin);
            markers.push(marker);
            bounds.extend(origin.location);
        }
    }

    // Add markers for the destinations.
    for (const destination of destinations) {
        if (destination.location) {
            const pin = new PinElement({
                glyphText: 'D',
                glyphColor: 'white',
                background: '#C5221F',
                borderColor: 'white',
            });

            const marker = new AdvancedMarkerElement({
                map,
                position: destination.location,
                content: pin,
                title: `Destination: ${destination.displayName ?? 'Unknown'}`,
            });

            markers.push(marker);
            bounds.extend(destination.location);
        }
    }

    // Fit the map to the bounds of all markers.
    map.fitBounds(bounds);
}

void init();

CSS

/*
 * Always set the map height explicitly to define the size of the div element
 * that contains the map.
 */
/* Optional: Makes the sample page fill the window. */
html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
}

#container {
    height: 100%;
    display: flex;
}

#sidebar {
    flex-basis: 15rem;
    flex-grow: 1;
    padding: 1rem;
    max-width: 30rem;
    height: 100%;
    box-sizing: border-box;
    overflow: auto;
}

#map {
    flex-basis: 0;
    flex-grow: 4;
    height: 100%;
}

#sidebar {
    flex-direction: column;
}

HTML

<html>
    <head>
        <title>Route matrix</title>

        <link rel="stylesheet" type="text/css" href="./style.css" />
        <script type="module" src="./index.js"></script>
        <script>
            // prettier-ignore
            (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
                key: "GOOGLE_MAPS_API_KEY"
            });
        </script>
    </head>
    <body>
        <div id="container">
            <div id="map"></div>
            <div id="sidebar">
                <h3 style="flex-grow: 0">Request</h3>
                <pre style="flex-grow: 1" id="request"></pre>
                <h3 style="flex-grow: 0">Response</h3>
                <pre style="flex-grow: 1" id="response"></pre>
            </div>
        </div>
    </body>
</html>

স্যাম্পেল ব্যবহার করে দেখুন

অনুরোধের সীমা

computeRouteMatrix পদ্ধতিটি ঠিকানা বা স্থানের ইনস্ট্যান্স ব্যবহার করে ওয়েপয়েন্টের জন্য এবং আইটেমের জন্য নিম্নলিখিত অনুরোধের সীমা এনফোর্স করে। আইটেম হল রুট ম্যাট্রিক্সে প্রতিটি উৎস ও গন্তব্যের মধ্যে থাকা রুট, তাই আইটেমের সংখ্যা হল উৎসের সংখ্যা ও গন্তব্যের সংখ্যার গুণফল। যেমন, আপনার কাছে ১০টি উৎস এবং ১০টি গন্তব্য থাকলে, আপনার কাছে ১০০টি আইটেম আছে:

  • TRANSIT রুট নয় এমন রুটের ক্ষেত্রে আইটেমের সংখ্যা ৬২৫-এর বেশি হতে পারবে না।
  • আপনি TRANSIT রুট উল্লেখ করলে, আইটেমের সংখ্যা ১০০-এর বেশি হতে পারবে না।
  • আপনি TRAFFIC_AWARE_OPTIMAL উল্লেখ করলে, আইটেমের সংখ্যা ১০০-এর বেশি হতে পারবে না।
  • আপনি যদি ঠিকানা বা জায়গার ইনস্ট্যান্স ব্যবহার করে উৎস বা গন্তব্য নির্দিষ্ট করেন, তাহলে আপনি এইভাবে মোট ৫০টি পর্যন্ত নির্দিষ্ট করতে পারবেন।

আরও তথ্যের জন্য, ট্রানজিট রুট পান দেখুন।

রুট ম্যাট্রিক্স অনুরোধের উদাহরণ

নিচের উদাহরণে একটি ComputeRouteMatrixRequest দেখানো হয়েছে। এই উদাহরণ নিম্নলিখিত কাজ করে:

  • দুটি উৎস ও দুটি গন্তব্যের ওয়েপয়েন্টের অ্যারে নির্দিষ্ট করে দেখায়। পদ্ধতিটি প্রতিটি উৎস থেকে প্রতিটি গন্তব্যে একটি রুট গণনা করে যাতে প্রতিক্রিয়াটিতে চারটি রুট থাকে।
    অ্যারেতে, প্রথম এলিমেন্ট ০ ইন্ডেক্সে, দ্বিতীয় এলিমেন্ট ১ ইন্ডেক্সে এবং এইভাবে বাকিগুলি থাকে।
  • ফেরত পাঠানোর জন্য ফিল্ড নির্দিষ্ট করে। এই উদাহরণে, প্রতিটি রুটের জন্য durationMillis, distanceMeters এবং condition ফেরত দেওয়ার জন্য অনুরোধটি কনফিগার করা হয়েছে।

TypeScript

const destinations = [destinationA, destinationB];
const origins = [origin1, origin2];
const request: google.maps.routes.ComputeRouteMatrixRequest = {
    origins,
    destinations,
    travelMode: 'DRIVING',
    units: UnitSystem.METRIC,
    fields: ['distanceMeters', 'durationMillis', 'condition'],
};

জাভাস্ক্রিপ্ট

const destinations = [destinationA, destinationB];
const origins = [origin1, origin2];
const request = {
    origins,
    destinations,
    travelMode: 'DRIVING',
    units: UnitSystem.METRIC,
    fields: ['distanceMeters', 'durationMillis', 'condition'],
};

উত্তরে সমস্ত উৎস এবং গন্তব্য ওয়েপয়েন্টের সংমিশ্রণের জন্য চারটি সম্ভাব্য রুট রয়েছে, যেমনটি নিম্নলিখিত উদাহরণে দেখানো হয়েছে:

"matrix": {
  "rows": [
    {
      "items": [
        {
          "condition": "ROUTE_EXISTS",
          "distanceMeters": 202587,
          "durationMillis": 10040000
        },
        {
          "condition": "ROUTE_EXISTS",
          "distanceMeters": 252734,
          "durationMillis": 12240000
        }
      ]
    },
    {
      "items": [
        {
          "condition": "ROUTE_EXISTS",
          "distanceMeters": 166135,
          "durationMillis": 6596000
        },
        {
          "condition": "ROUTE_EXISTS",
          "distanceMeters": 216282,
          "durationMillis": 8797000
        }
      ]
    }
  ]
}
    

ফলাফলে প্রতিটি রুট শনাক্ত করতে, শুরু ও গন্তব্যের ইন্ডেক্স ব্যবহার করে 2D অ্যারেতে সংশ্লিষ্ট RouteMatrixItem খুঁজে নিন। যেমন, অনুরোধে ইন্ডেক্স ১-এ থাকা উৎস এবং গন্তব্য ০ থেকে গণনা করা রুটের বিবরণ দেওয়া RouteMatrixItem RouteMatrix.rows অ্যারের ২য় এলিমেন্ট এবং RouteMatrixRow.items অ্যারের ১ম এলিমেন্টে থাকবে।

নির্দিষ্ট উৎস ও গন্তব্যের জন্য রুট খুঁজে পেতে কীভাবে RouteMatrixItem শনাক্ত করতে হয় তা নিম্নলিখিত কোড স্নিপেট থেকে জানতে পারবেন:

// Find the route for origin 'x' and destination 'y'.
const {matrix} = await RouteMatrix.computeRouteMatrix(request);
const myRouteMatrixItem = matrix.rows[x].items[y];
    

ফেরত দেওয়ার জন্য ফিল্ড বেছে নিন

আপনি রুট ম্যাট্রিক্সের অনুরোধ করলে, উত্তরে কী তথ্য দেখানো হবে তা নির্দিষ্ট করতে আপনাকে অবশ্যই ফিল্ড মাস্ক ব্যবহার করতে হবে।

এছাড়াও, ফিল্ড মাস্ক ব্যবহার করলে আপনি অপ্রয়োজনীয় ডেটার জন্য অনুরোধ করবেন না, যা রেসপন্স লেটেন্সির ক্ষেত্রে সাহায্য করে এবং আপনার সিস্টেমের প্রয়োজন নেই এমন তথ্য ফেরত পাঠানো এড়ায়।

আপনার প্রয়োজনীয় ফিল্ডের তালিকা নির্দিষ্ট করতে ComputeRoutesMatrixRequest.fields প্রপার্টি সেট করুন, যেমনটি নিম্নলিখিত স্নিপেটে দেখানো হয়েছে:

fields: ['durationMillis', 'distanceMeters', 'condition'],
    

কোন ফিল্ড মাস্ক ব্যবহার করতে হবে তা নির্ধারণ করুন

আপনি কোন ফিল্ড ব্যবহার করতে চান তা কীভাবে নির্ধারণ করবেন এবং সেগুলির জন্য ফিল্ড মাস্ক তৈরি করবেন তা এখানে দেওয়া হল:

  1. ['*']-এর ফিল্ড মাস্ক ব্যবহার করে সব ফিল্ডের জন্য অনুরোধ করুন।
  2. আপনি যে ফিল্ড চান তার জন্য RouteMatrixItem ক্লাসে ফিল্ডের হায়ারার্কি দেখুন।
  3. আগের ধাপে দেখানো ফিল্ডের ক্রম অনুযায়ী এই ফর্ম্যাট ব্যবহার করে আপনার ফিল্ড মাস্ক তৈরি করুন:

    topLevelField[.secondLevelField][.thirdLevelField][...]

যেমন, এই RouteMatrixItem-এর জন্য:

  "travelAdvisory": {
    "fuelConsumptionMicroliters": 0,
    "tollInfo": {
      "estimatedPrices": [
        {
          "currencyCode": "USD",
          "units": 4,
          "nanos": 400000000
        }
      ]
    }
  },
    

আপনি যদি RouteMatrixItem-এর জন্য শুধু tollInfo ফিল্ডটি ফেরত পেতে চান, তাহলে আপনার ফিল্ড মাস্কটি এইরকম হবে:

fields: ['travelAdvisory.tollInfo']

পরিবর্তে আপনি যদি আনুমানিক জ্বালানি খরচের অনুরোধ করতে চান, তাহলে আপনার ফিল্ড মাস্ক নিম্নলিখিত হবে:

fields: ['travelAdvisory.fuelConsumptionMicroliters']

আপনি যদি দুটির জন্যই অনুরোধ করতে চান, তাহলে আপনার ফিল্ড মাস্ক নিম্নলিখিত হবে:

fields: ['travelAdvisory.fuelConsumptionMicroliters', 'travelAdvisory.tollInfo']

এবং আপনি যদি ভ্রমণ সংক্রান্ত সতর্কতার সম্পূর্ণ সেট অনুরোধ করতে চান, তাহলে আপনার ফিল্ড মাস্ক নিম্নলিখিত:

fields: ['travelAdvisory']

ট্রানজিট রুট ম্যাট্রিক্সের অনুরোধ করা

একটি ট্রানজিট রুট ম্যাট্রিক্স পান যা অঞ্চলে উপলভ্য পাবলিক ট্রান্সপোর্ট বিকল্পগুলি ব্যবহার করে। ট্রানজিট বিকল্পের মধ্যে বাস, পাতাল রেল ও ট্রেন সহ অন্যান্য বিকল্প থাকতে পারে। ট্রানজিট রুট ম্যাট্রিক্সের অনুরোধ করতে:

  • travelMode-কে TRANSIT-এ সেট করুন
  • travelAdvisory ফিল্ডের জন্য অনুরোধ করুন।

ট্রানজিট রুট সম্পর্কে আরও জানুন।