createWaypointAdvancedMarkers() কল করে রুটের ওয়েপয়েন্টের জন্য মার্কার তৈরি করুন।
ডিফল্ট হিসেবে, createWaypointAdvancedMarkers() প্রতিটি ওয়েপয়েন্টের জন্য
'A', 'B', 'C' ইত্যাদি লেবেল সহ রুটের জন্য মার্কার তৈরি করে। আপনি মার্কার ইনডেক্স বা সংশ্লিষ্ট RouteLeg-এর প্রপার্টির উপর ভিত্তি করে মার্কার স্টাইল পরিবর্তন করার জন্য বিকল্প পাস করে মার্কার আরও কাস্টমাইজ করতে পারেন।
সম্পূর্ণ উদাহরণ সোর্স কোড দেখুন
রুট ওয়েপয়েন্টের জন্য কীভাবে কাস্টম মার্কার তৈরি করতে হয় তা নিম্নলিখিত কোড স্যাম্পেল থেকে জানতে পারবেন।
TypeScript
let mapPolylines: google.maps.Polyline[] = []; const mapElement = document.querySelector('gmp-map')!; let innerMap: google.maps.Map; // Initialize and add the map. async function init() { // Request the needed libraries. const [{ event }] = await Promise.all([ google.maps.importLibrary('core'), google.maps.importLibrary('maps'), ]); innerMap = mapElement.innerMap; innerMap.setOptions({ mapTypeControl: false, mapId: 'DEMO_MAP_ID', }); // Call the function after the map is loaded. event.addListenerOnce(innerMap, 'idle', () => { void getDirections(); }); } async function getDirections() { // Request the needed libraries. const [{ Route }, { PinElement }] = await Promise.all([ google.maps.importLibrary('routes'), google.maps.importLibrary('marker'), ]); // Define routes request with an intermediate stop. const request: google.maps.routes.ComputeRoutesRequest = { origin: 'Parking lot, Christmas Tree Point Rd, San Francisco, CA 94131', destination: '100 Spinnaker Dr, Sausalito, CA 94965', // We're having a yummy lunch! intermediates: [{ location: '300 Finley Rd San Francisco, CA 94129' }], // But first, we golf! travelMode: 'DRIVING', fields: ['path', 'legs', 'viewport'], }; // Call computeRoutes to get the directions. const result = await Route.computeRoutes(request); if (!result.routes || result.routes.length === 0) { console.warn('No routes found'); return; } // Alter style based on marker index. function markerOptionsMaker( defaultOptions: google.maps.marker.AdvancedMarkerElementOptions, waypointMarkerDetails: google.maps.routes.WaypointMarkerDetails ) { const { index, totalMarkers } = waypointMarkerDetails; // Style the origin waypoint. if (index === 0) { return { ...defaultOptions, title: 'Origin', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'green', borderColor: 'green', scale: 1.5, }), }; } // Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; } // Style the destination waypoint. if (index === totalMarkers - 1) { return { ...defaultOptions, title: 'Destination', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'red', borderColor: 'red', scale: 1.5, }), }; } return { ...defaultOptions, map: innerMap }; } await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker); // Fit the map to the route. innerMap.fitBounds(result.routes[0].viewport!); innerMap.setHeading(270); // Create polylines and add them to the map. mapPolylines = result.routes[0].createPolylines(); mapPolylines.forEach((polyline) => { polyline.setMap(innerMap); }); } void init();
জাভাস্ক্রিপ্ট
let mapPolylines = []; const mapElement = document.querySelector('gmp-map'); let innerMap; // Initialize and add the map. async function init() { // Request the needed libraries. const [{ event }] = await Promise.all([ google.maps.importLibrary('core'), google.maps.importLibrary('maps'), ]); innerMap = mapElement.innerMap; innerMap.setOptions({ mapTypeControl: false, mapId: 'DEMO_MAP_ID', }); // Call the function after the map is loaded. event.addListenerOnce(innerMap, 'idle', () => { void getDirections(); }); } async function getDirections() { // Request the needed libraries. const [{ Route }, { PinElement }] = await Promise.all([ google.maps.importLibrary('routes'), google.maps.importLibrary('marker'), ]); // Define routes request with an intermediate stop. const request = { origin: 'Parking lot, Christmas Tree Point Rd, San Francisco, CA 94131', destination: '100 Spinnaker Dr, Sausalito, CA 94965', // We're having a yummy lunch! intermediates: [{ location: '300 Finley Rd San Francisco, CA 94129' }], // But first, we golf! travelMode: 'DRIVING', fields: ['path', 'legs', 'viewport'], }; // Call computeRoutes to get the directions. const result = await Route.computeRoutes(request); if (!result.routes || result.routes.length === 0) { console.warn('No routes found'); return; } // Alter style based on marker index. function markerOptionsMaker(defaultOptions, waypointMarkerDetails) { const { index, totalMarkers } = waypointMarkerDetails; // Style the origin waypoint. if (index === 0) { return { ...defaultOptions, title: 'Origin', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'green', borderColor: 'green', scale: 1.5, }), }; } // Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; } // Style the destination waypoint. if (index === totalMarkers - 1) { return { ...defaultOptions, title: 'Destination', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'red', borderColor: 'red', scale: 1.5, }), }; } return { ...defaultOptions, map: innerMap }; } await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker); // Fit the map to the route. innerMap.fitBounds(result.routes[0].viewport); innerMap.setHeading(270); // Create polylines and add them to the map. mapPolylines = result.routes[0].createPolylines(); mapPolylines.forEach((polyline) => { polyline.setMap(innerMap); }); } void init();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
HTML
<html>
<head>
<title>Get directions</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>
<gmp-map center="37.447646, -122.113878" zoom="12"></gmp-map>
</body>
</html>স্যাম্পেল ব্যবহার করে দেখুন
ডিফল্ট মার্কার যোগ করুন
ডিফল্ট স্টাইল ব্যবহার করে মার্কার যোগ করতে, createWaypointAdvancedMarkers()
কল করুন এবং বর্তমান ম্যাপ নির্দিষ্ট করার জন্য মার্কার বিকল্প পাস করুন, যেমনটি নিম্নলিখিত কোড স্নিপেটে দেখানো হয়েছে।
// Create markers using default style. const markers = await result.routes[0].createWaypointAdvancedMarkers({ map: innerMap });
কাস্টম স্টাইল সহ মার্কার যোগ করুন
কাস্টম স্টাইল ব্যবহার করে মার্কার যোগ করতে, createWaypointAdvancedMarkers()
কল করুন, কাস্টম স্টাইল প্রপার্টি সহ মার্কার বিকল্প পাস করুন। আপনি এইভাবে স্টাইল প্রয়োগ করলে,
সব মার্কার একই স্টাইলে দেখানো হয়। কাস্টম স্টাইল সহ কীভাবে মার্কার তৈরি করতে হয় তা নিম্নলিখিত কোড স্নিপেট থেকে
জানতে পারবেন।
// Create markers with a custom style. const markerOptions = { map: innerMap, content: new PinElement({ scale: 1.5, background: '#8C0DD1', borderColor: '#6D0AA5', glyphColor: '#6D0AA5', }).element } const markers = await result.routes[0].createWaypointAdvancedMarkers(markerOptions);
আলাদা আলাদা মার্কারের জন্য কাস্টম স্টাইল প্রয়োগ করুন
আলাদা আলাদা মার্কারের জন্য কাস্টম স্টাইল প্রয়োগ করতে, মার্কার ইন্ডেক্স বা RouteLeg প্রপার্টির উপর ভিত্তি করে মার্কার স্টাইল বিকল্প সেট করতে createWaypointAdvancedMarkers()-এ একটি ফাংশন পাস করুন।
নিচের কোড স্নিপেট থেকে মার্কার তৈরি করার ফাংশন এবং মার্কার ইন্ডেক্সের ভিত্তিতে
সেগুলি স্টাইল করার পদ্ধতি দেখানো হয়েছে:
TypeScript
// Alter style based on marker index. function markerOptionsMaker( defaultOptions: google.maps.marker.AdvancedMarkerElementOptions, waypointMarkerDetails: google.maps.routes.WaypointMarkerDetails ) { const { index, totalMarkers } = waypointMarkerDetails; // Style the origin waypoint. if (index === 0) { return { ...defaultOptions, title: 'Origin', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'green', borderColor: 'green', scale: 1.5, }), }; } // Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; } // Style the destination waypoint. if (index === totalMarkers - 1) { return { ...defaultOptions, title: 'Destination', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'red', borderColor: 'red', scale: 1.5, }), }; } return { ...defaultOptions, map: innerMap }; } await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker);
জাভাস্ক্রিপ্ট
// Alter style based on marker index. function markerOptionsMaker(defaultOptions, waypointMarkerDetails) { const { index, totalMarkers } = waypointMarkerDetails; // Style the origin waypoint. if (index === 0) { return { ...defaultOptions, title: 'Origin', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'green', borderColor: 'green', scale: 1.5, }), }; } // Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; } // Style the destination waypoint. if (index === totalMarkers - 1) { return { ...defaultOptions, title: 'Destination', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'red', borderColor: 'red', scale: 1.5, }), }; } return { ...defaultOptions, map: innerMap }; } await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker);
মার্কার অ্যাক্সেসযোগ্য করুন
আপনি নিম্নলিখিত উপায়ে মার্কার অ্যাক্সেসিবিলিটি উন্নত করতে পারবেন:
- একটি শীর্ষক প্রদান করুন: মার্কারের জন্য একটি লেবেল প্রদান করতে মার্কার বিকল্পে
titleপ্রপার্টি সেট করুন। এই লেবেল স্ক্রিন রিডার ও সহায়ক প্রযুক্তি ব্যবহার করে। - মধ্যবর্তী ওয়েপয়েন্ট আলাদা করা:
createWaypointAdvancedMarkers()-এ স্টাইল ফাংশন পাস করার সময়,WaypointMarkerDetailsথেকেindexএবংtotalMarkersপ্যারামিটার ব্যবহার করে মধ্যবর্তী ওয়েপয়েন্টকে অনন্য, বর্ণনামূলক শীর্ষক এবং লেবেল অ্যাসাইন করুন (যেমন,Waypoint ${(index + 1).toString()}) যাতে স্ক্রিন রিডার ব্যবহারকারীরা সেগুলিকে শুরু এবং গন্তব্য থেকে আলাদা করতে পারেন। - কীবোর্ড নেভিগেশন ও ক্লিক ইভেন্ট চালু করুন: মার্কার বিকল্পে
gmpClickable: trueসেট করুন যাতে ইন্টার্যাক্টিভ মার্কার কীবোর্ডের (ট্যাব ও তীরচিহ্ন কী ব্যবহার করে) মাধ্যমে ফোকাস করা যায় এবং কীবোর্ড ও মাউস ইনপুটে রেসপন্স করতে পারে। - মার্কারের সাইজ বাড়ান:
PinElement-এscaleপ্রপার্টি সেট করুন (যেমন,scale: 1.5) যাতে মার্কারের সাথে ইন্টার্যাক্ট করার জন্য প্রয়োজনীয় প্রিসিশন কমানো যায়, বিশেষ করে টাচস্ক্রিন ডিভাইসে। ডিফল্ট মার্কার WCAG AA ন্যূনতম সাইজ স্ট্যান্ডার্ড পূরণ করে এবং স্কেল বাড়ালে তা WCAG AAA টার্গেট সাইজ স্ট্যান্ডার্ড পূরণ করতে সাহায্য করে। - কালার কনট্রাস্ট বজায় রাখুন:
PinElementকালার কাস্টমাইজ করার সময়, পর্যাপ্ত কনট্রাস্ট সহglyphColorএবংbackgroundভ্যালু বেছে নিন যাতে ওয়েপয়েন্ট লেবেল পড়া যায়।
অ্যাক্সেসিবিলিটির জন্য মার্কার কীভাবে স্টাইল করতে হয় তা নিম্নলিখিত কোড স্নিপেট থেকে জানতে পারবেন। স্ক্রিন রিডারের জন্য লেবেল প্রদান করতে এতে
title আছে; মধ্যবর্তী ওয়েপয়েন্ট আলাদা করতে
টাইটেল ইন্ডেক্স ব্যবহার করে। কীবোর্ড নেভিগেশন ও ক্লিক ইভেন্ট চালু করতে gmpClickable প্রপার্টি true-এ সেট করা হয়েছে এবং মার্কারের সাথে ইন্টার্যাক্ট করা আরও সহজ করতে scale বাড়ানো হয়েছে।
TypeScript
// Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; }
জাভাস্ক্রিপ্ট
// Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; }
আরও জানতে, মার্কার ক্লিকযোগ্য ও অ্যাক্সেসযোগ্য করুন।
3D ম্যাপে ওয়েপয়েন্ট মার্কার যোগ করা
v=beta চ্যানেলে,
<gmp-route-3d>
এলিমেন্টটি অটোমেটিক রুট পলিলাইন এবং <gmp-map-3d>
এলিমেন্টে সারসংক্ষেপ পপওভারের পাশাপাশি
শুরু ও গন্তব্য মার্কার রেন্ডার করে—
3D ম্যাপে রুট দেখুন।
প্রোগ্রাম্যাটিক্যালি Route ইনস্ট্যান্স রেন্ডার করার সময়,
ComputeRoutesRequest.fields-এ 'path'-এর অনুরোধ করুন এবং
<gmp-map-3d> এলিমেন্টের জন্য
শুরু ও গন্তব্যের মার্কার তৈরি করতে
createWaypointMarkers কল করুন। ডিফল্ট হিসেবে,
createWaypointMarkers
Marker3DElement
ইন্সট্যান্স রিটার্ন করে, যেগুলি 'A' ও 'B' হিসেবে লেবেল করা থাকে। বিটা চ্যানেল লোড করার
নির্দেশাবলীর জন্য,
ভার্সন করা দেখুন। নিম্নলিখিত উদাহরণটি 3D ওয়েপয়েন্ট মার্কার তৈরি করে এবং
যোগ করে:
const markers3D = await result.routes[0].createWaypointMarkers(); map3DElement.append(...markers3D);
সম্পূর্ণ উদাহরণ দেখুন
TypeScript
let mapPolylines: google.maps.Polyline[] = []; const mapElement = document.querySelector('gmp-map')!; let innerMap: google.maps.Map; // Initialize and add the map. async function init() { // Request the needed libraries. const [{ event }] = await Promise.all([ google.maps.importLibrary('core'), google.maps.importLibrary('maps'), ]); innerMap = mapElement.innerMap; innerMap.setOptions({ mapTypeControl: false, mapId: 'DEMO_MAP_ID', }); // Call the function after the map is loaded. event.addListenerOnce(innerMap, 'idle', () => { void getDirections(); }); } async function getDirections() { // Request the needed libraries. const [{ Route }, { PinElement }] = await Promise.all([ google.maps.importLibrary('routes'), google.maps.importLibrary('marker'), ]); // Define routes request with an intermediate stop. const request: google.maps.routes.ComputeRoutesRequest = { origin: 'Parking lot, Christmas Tree Point Rd, San Francisco, CA 94131', destination: '100 Spinnaker Dr, Sausalito, CA 94965', // We're having a yummy lunch! intermediates: [{ location: '300 Finley Rd San Francisco, CA 94129' }], // But first, we golf! travelMode: 'DRIVING', fields: ['path', 'legs', 'viewport'], }; // Call computeRoutes to get the directions. const result = await Route.computeRoutes(request); if (!result.routes || result.routes.length === 0) { console.warn('No routes found'); return; } // Alter style based on marker index. function markerOptionsMaker( defaultOptions: google.maps.marker.AdvancedMarkerElementOptions, waypointMarkerDetails: google.maps.routes.WaypointMarkerDetails ) { const { index, totalMarkers } = waypointMarkerDetails; // Style the origin waypoint. if (index === 0) { return { ...defaultOptions, title: 'Origin', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'green', borderColor: 'green', scale: 1.5, }), }; } // Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; } // Style the destination waypoint. if (index === totalMarkers - 1) { return { ...defaultOptions, title: 'Destination', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'red', borderColor: 'red', scale: 1.5, }), }; } return { ...defaultOptions, map: innerMap }; } await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker); // Fit the map to the route. innerMap.fitBounds(result.routes[0].viewport!); innerMap.setHeading(270); // Create polylines and add them to the map. mapPolylines = result.routes[0].createPolylines(); mapPolylines.forEach((polyline) => { polyline.setMap(innerMap); }); } void init();
জাভাস্ক্রিপ্ট
let mapPolylines = []; const mapElement = document.querySelector('gmp-map'); let innerMap; // Initialize and add the map. async function init() { // Request the needed libraries. const [{ event }] = await Promise.all([ google.maps.importLibrary('core'), google.maps.importLibrary('maps'), ]); innerMap = mapElement.innerMap; innerMap.setOptions({ mapTypeControl: false, mapId: 'DEMO_MAP_ID', }); // Call the function after the map is loaded. event.addListenerOnce(innerMap, 'idle', () => { void getDirections(); }); } async function getDirections() { // Request the needed libraries. const [{ Route }, { PinElement }] = await Promise.all([ google.maps.importLibrary('routes'), google.maps.importLibrary('marker'), ]); // Define routes request with an intermediate stop. const request = { origin: 'Parking lot, Christmas Tree Point Rd, San Francisco, CA 94131', destination: '100 Spinnaker Dr, Sausalito, CA 94965', // We're having a yummy lunch! intermediates: [{ location: '300 Finley Rd San Francisco, CA 94129' }], // But first, we golf! travelMode: 'DRIVING', fields: ['path', 'legs', 'viewport'], }; // Call computeRoutes to get the directions. const result = await Route.computeRoutes(request); if (!result.routes || result.routes.length === 0) { console.warn('No routes found'); return; } // Alter style based on marker index. function markerOptionsMaker(defaultOptions, waypointMarkerDetails) { const { index, totalMarkers } = waypointMarkerDetails; // Style the origin waypoint. if (index === 0) { return { ...defaultOptions, title: 'Origin', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'green', borderColor: 'green', scale: 1.5, }), }; } // Style all intermediate waypoints. if (!(index === 0 || index === totalMarkers - 1)) { return { ...defaultOptions, title: `Waypoint ${(index + 1).toString()}`, // Provide label for screenreaders map: innerMap, gmpClickable: true, // Make marker focusable and allow keyboard navigation content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'blue', borderColor: 'blue', scale: 1.5, // Increase size for better visibilty }), }; } // Style the destination waypoint. if (index === totalMarkers - 1) { return { ...defaultOptions, title: 'Destination', map: innerMap, gmpClickable: true, content: new PinElement({ glyphText: (index + 1).toString(), glyphColor: 'white', background: 'red', borderColor: 'red', scale: 1.5, }), }; } return { ...defaultOptions, map: innerMap }; } await result.routes[0].createWaypointAdvancedMarkers(markerOptionsMaker); // Fit the map to the route. innerMap.fitBounds(result.routes[0].viewport); innerMap.setHeading(270); // Create polylines and add them to the map. mapPolylines = result.routes[0].createPolylines(); mapPolylines.forEach((polyline) => { polyline.setMap(innerMap); }); } void init();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
HTML
<html>
<head>
<title>Get directions</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>
<gmp-map center="37.447646, -122.113878" zoom="12"></gmp-map>
</body>
</html>