রুট ম্যাট্রিক্স হলো পথের তথ্যের একটি দ্বি-মাত্রিক অ্যারে, যেখানে সারিগুলো যাত্রার উৎস এবং কলামগুলো গন্তব্যস্থল নির্দেশ করে। উৎস এবং গন্তব্যস্থলের একটি তালিকা দেওয়া থাকলে, রুট ম্যাট্রিক্স ক্লাসটি প্রতিটি উৎস থেকে শুরু হয়ে প্রতিটি গন্তব্যে শেষ হওয়া একটি পথের দূরত্ব এবং সময়কাল গণনা করে। একাধিক উৎস এবং গন্তব্যস্থলের জন্য একটি পথের দূরত্ব এবং সময়কাল গণনা করতে রুট ম্যাট্রিক্স ক্লাসটি ব্যবহার করুন।
সম্পূর্ণ উদাহরণ সোর্স কোড দেখুন
এই উদাহরণে দেখানো হয়েছে কীভাবে একাধিক উৎস ও গন্তব্যের মধ্যে ভ্রমণের দূরত্ব এবং সময়কাল গণনা করতে Route Matrix ক্লাস ব্যবহার করতে হয়।
টাইপস্ক্রিপ্ট
// 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();
সিএসএস
/* * 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>
<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 মেথডটি `address` বা `Place` ইনস্ট্যান্স ব্যবহার করে ওয়েপয়েন্ট এবং আইটেমগুলির জন্য নিম্নলিখিত অনুরোধ সীমা প্রয়োগ করে। আইটেমগুলি হলো একটি রুট ম্যাট্রিক্সের প্রতিটি উৎস এবং গন্তব্যের মধ্যবর্তী রুট, তাই আইটেমের সংখ্যা হলো উৎসের সংখ্যা এবং গন্তব্যের সংখ্যার গুণফল। উদাহরণস্বরূপ, যদি আপনার ১০টি উৎস এবং ১০টি গন্তব্য থাকে, তাহলে আপনার ১০০টি আইটেম থাকবে:
- যেসব রুট
TRANSITরুট নয়, সেগুলোর ক্ষেত্রে আইটেমের সংখ্যা ৬২৫-এর বেশি হতে পারবে না। - আপনি যদি একটি
TRANSITরুট নির্দিষ্ট করেন , তাহলে আইটেমের সংখ্যা ১০০-এর বেশি হতে পারবে না। - আপনি যদি
TRAFFIC_AWARE_OPTIMALনির্দিষ্ট করেন , তাহলে আইটেমের সংখ্যা ১০০-এর বেশি হতে পারবে না। - যদি আপনি অ্যাড্রেস বা প্লেস ইনস্ট্যান্স ব্যবহার করে উৎস বা গন্তব্য নির্দিষ্ট করেন , তাহলে এই পদ্ধতিতে আপনি মোট ৫০টি পর্যন্ত নির্দিষ্ট করতে পারবেন।
অতিরিক্ত বিবরণের জন্য, ‘একটি গণপরিবহন রুট নিন’ দেখুন।
উদাহরণ রুট ম্যাট্রিক্স অনুরোধ
নিম্নলিখিত উদাহরণটি একটি ComputeRouteMatrixRequest দেখায়। এই উদাহরণটি নিম্নলিখিত কাজগুলো করে:
- দুটি উৎস এবং দুটি গন্তব্য ওয়েপয়েন্টের একটি অ্যারে নির্দিষ্ট করা দেখানো হচ্ছে। পদ্ধতিটি প্রতিটি উৎস থেকে প্রতিটি গন্তব্যে যাওয়ার জন্য একটি রুট গণনা করে, ফলে প্রতিক্রিয়ায় চারটি রুট থাকে।
অ্যারেটিতে প্রথম উপাদানটির ইন্ডেক্স ০, দ্বিতীয়টির ইন্ডেক্স ১, এবং এভাবেই চলতে থাকে। - কোন কোন ফিল্ড ফেরত দেওয়া হবে তা নির্দিষ্ট করে। এই উদাহরণে, প্রতিটি রুটের জন্য
durationMillis,distanceMetersএবংconditionফেরত দেওয়ার জন্য অনুরোধটি কনফিগার করা হয়েছে।
টাইপস্ক্রিপ্ট
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'],
কোন ফিল্ড মাস্ক ব্যবহার করতে হবে তা নির্ধারণ করুন
আপনি কোন ফিল্ডগুলো ব্যবহার করতে চান তা নির্ধারণ করার এবং সেগুলোর জন্য ফিল্ড মাস্ক তৈরি করার পদ্ধতি নিচে দেওয়া হলো:
-
['*']ফিল্ড মাস্ক ব্যবহার করে সমস্ত ফিল্ডের জন্য অনুরোধ করুন । - আপনার কাঙ্ক্ষিত ফিল্ডগুলোর জন্য
RouteMatrixItemক্লাসের ফিল্ডগুলোর ক্রমবিন্যাস দেখুন । পূর্ববর্তী ধাপে দেখানো ফিল্ডগুলির ক্রমবিন্যাস ব্যবহার করে এই ফর্ম্যাট অনুসারে আপনার ফিল্ড মাস্কগুলি তৈরি করুন :
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ক্ষেত্রটির জন্য অনুরোধ করুন।
গণপরিবহনের রুটগুলো সম্পর্কে আরও জানুন।