কাস্টম ওভারলে

প্ল্যাটফর্ম নির্বাচন করুন: অ্যান্ড্রয়েড আইওএস জাভাস্ক্রিপ্ট

ভূমিকা

ওভারলে হলো মানচিত্রের এমন বস্তু যা অক্ষাংশ/দ্রাঘিমাংশ স্থানাঙ্কের সাথে আবদ্ধ, তাই যখন আপনি মানচিত্রটি টেনে আনবেন বা জুম করবেন তখন এগুলি সরে যাবে। পূর্বনির্ধারিত ওভারলে প্রকার সম্পর্কে তথ্যের জন্য, মানচিত্রে অঙ্কন দেখুন।

Maps JavaScript API আপনার নিজস্ব কাস্টম ওভারলে তৈরি করার জন্য একটি OverlayView ক্লাস প্রদান করে। OverlayView হল একটি বেস ক্লাস যা আপনার ওভারলে তৈরি করার সময় প্রয়োগ করার জন্য বেশ কয়েকটি পদ্ধতি প্রদান করে। ক্লাসটি এমন কয়েকটি পদ্ধতিও প্রদান করে যা মানচিত্রে স্ক্রিন স্থানাঙ্ক এবং অবস্থানের মধ্যে অনুবাদ করা সম্ভব করে।

একটি কাস্টম ওভারলে যোগ করুন

একটি কাস্টম ওভারলে তৈরি করার জন্য প্রয়োজনীয় পদক্ষেপগুলির একটি সারসংক্ষেপ এখানে দেওয়া হল:

  • আপনার কাস্টম ওভারলে অবজেক্টের prototype google.maps.OverlayView() এর একটি নতুন ইনস্ট্যান্সে সেট করুন। কার্যত, এটি ওভারলে ক্লাসকে সাবক্লাস করবে।
  • আপনার কাস্টম ওভারলে-এর জন্য একটি কনস্ট্রাক্টর তৈরি করুন এবং যেকোনো ইনিশিয়ালাইজেশন প্যারামিটার সেট করুন।
  • আপনার প্রোটোটাইপের মধ্যে একটি onAdd() পদ্ধতি প্রয়োগ করুন এবং ম্যাপের সাথে ওভারলে সংযুক্ত করুন। ওভারলে সংযুক্ত করার জন্য ম্যাপ প্রস্তুত হলে OverlayView.onAdd() কল করা হবে।
  • তোমার প্রোটোটাইপের মধ্যে একটি draw() পদ্ধতি প্রয়োগ করো, এবং তোমার অবজেক্টের ভিজ্যুয়াল ডিসপ্লে পরিচালনা করো। অবজেক্টটি প্রথম প্রদর্শিত হলে OverlayView.draw() কল করা হবে।
  • ওভারলেতে যোগ করা যেকোনো উপাদান পরিষ্কার করার জন্য আপনার একটি onRemove() পদ্ধতিও প্রয়োগ করা উচিত।

নীচে প্রতিটি ধাপ সম্পর্কে আরও বিশদ বিবরণ দেওয়া হল। আপনি সম্পূর্ণ, কার্যকর উদাহরণ কোডটি দেখতে পারেন: view example code

ওভারলে সাবক্লাস করুন

নিচের উদাহরণটি একটি সাধারণ চিত্র ওভারলে তৈরি করতে OverlayView ব্যবহার করে।

এখন আমরা USGSOverlay ক্লাসের জন্য একটি কনস্ট্রাক্টর তৈরি করব, এবং পাস করা প্যারামিটারগুলিকে নতুন অবজেক্টের বৈশিষ্ট্য হিসাবে ইনিশিয়ালাইজ করব।

টাইপস্ক্রিপ্ট

/**
 * The custom USGSOverlay object contains the USGS image,
 * the bounds of the image, and a reference to the map.
 */
class USGSOverlay extends google.maps.OverlayView {
  private bounds: google.maps.LatLngBounds;
  private image: string;
  private div?: HTMLElement;

  constructor(bounds: google.maps.LatLngBounds, image: string) {
    super();

    this.bounds = bounds;
    this.image = image;
  }

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

/**
 * The custom USGSOverlay object contains the USGS image,
 * the bounds of the image, and a reference to the map.
 */
class USGSOverlay extends google.maps.OverlayView {
  bounds;
  image;
  div;
  constructor(bounds, image) {
    super();
    this.bounds = bounds;
    this.image = image;
  }

আমরা এখনও ওভারলে কনস্ট্রাক্টরের ম্যাপে এই ওভারলেটি সংযুক্ত করতে পারছি না। প্রথমে, আমাদের নিশ্চিত করতে হবে যে ম্যাপের সমস্ত প্যানগুলি উপলব্ধ আছে, কারণ তারা ম্যাপে কোন ক্রমে বস্তুগুলি প্রদর্শিত হবে তা নির্দিষ্ট করে। API একটি সহায়ক পদ্ধতি প্রদান করে যা নির্দেশ করে যে এটি ঘটেছে। আমরা পরবর্তী বিভাগে সেই পদ্ধতিটি পরিচালনা করব।

ওভারলে শুরু করুন

যখন ওভারলেটি প্রথমে ইনস্ট্যান্টিয়েটেড হয় এবং প্রদর্শনের জন্য প্রস্তুত হয়, তখন আমাদের ব্রাউজারের DOM এর মাধ্যমে এটি ম্যাপে সংযুক্ত করতে হবে। API নির্দেশ করে যে ওভারলেটির onAdd() পদ্ধতি ব্যবহার করে ম্যাপে ওভারলে যোগ করা হয়েছে। এই পদ্ধতিটি পরিচালনা করার জন্য আমরা একটি <div> তৈরি করি যাতে আমাদের ছবি ধরে রাখা যায়, একটি <img> উপাদান যোগ করি, এটি <div> এর সাথে সংযুক্ত করি, এবং তারপর ওভারলেটিকে ম্যাপের একটি প্যানে সংযুক্ত করি। একটি প্যান হল DOM ট্রির মধ্যে একটি নোড।

MapPanes ধরণের প্যানগুলি মানচিত্রের বিভিন্ন স্তরের জন্য স্ট্যাকিং ক্রম নির্দিষ্ট করে। নিম্নলিখিত প্যানগুলি উপলব্ধ, এবং নীচে থেকে উপরে যে ক্রমে স্ট্যাক করা হয়েছে সেভাবেই তালিকাভুক্ত করা হয়েছে:

  • mapPane হল সর্বনিম্ন ফলক এবং টাইলসের উপরে অবস্থিত। এটি DOM ইভেন্ট নাও পেতে পারে। (ফলক ০)।
  • overlayLayer পলিলাইন, বহুভুজ, গ্রাউন্ড ওভারলে এবং টাইল লেয়ার ওভারলে রয়েছে। এটি DOM ইভেন্ট নাও পেতে পারে। (প্যান ১)।
  • markerLayer মার্কার আছে। এটি DOM ইভেন্ট নাও পেতে পারে। (প্যান ২)।
  • overlayMouseTarget এমন উপাদান রয়েছে যা DOM ইভেন্ট গ্রহণ করে। (প্যান 3)।
  • floatPane তথ্য উইন্ডো রয়েছে। এটি সমস্ত মানচিত্রের ওভারলেগুলির উপরে। (পেন ৪)।

যেহেতু আমাদের ছবিটি একটি "গ্রাউন্ড ওভারলে", তাই আমরা overlayLayer প্যানটি ব্যবহার করব। যখন আমাদের কাছে সেই প্যানটি থাকবে, তখন আমরা আমাদের অবজেক্টটিকে ছোটবেলায় এটির সাথে সংযুক্ত করব।

টাইপস্ক্রিপ্ট

/**
 * onAdd is called when the map's panes are ready and the overlay has been
 * added to the map.
 */
onAdd() {
  this.div = document.createElement("div");
  this.div.style.borderStyle = "none";
  this.div.style.borderWidth = "0px";
  this.div.style.position = "absolute";

  // Create the img element and attach it to the div.
  const img = document.createElement("img");

  img.src = this.image;
  img.style.width = "100%";
  img.style.height = "100%";
  img.style.position = "absolute";
  this.div.appendChild(img);

  // Add the element to the "overlayLayer" pane.
  const panes = this.getPanes()!;

  panes.overlayLayer.appendChild(this.div);
}

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

/**
 * onAdd is called when the map's panes are ready and the overlay has been
 * added to the map.
 */
onAdd() {
  this.div = document.createElement("div");
  this.div.style.borderStyle = "none";
  this.div.style.borderWidth = "0px";
  this.div.style.position = "absolute";

  // Create the img element and attach it to the div.
  const img = document.createElement("img");

  img.src = this.image;
  img.style.width = "100%";
  img.style.height = "100%";
  img.style.position = "absolute";
  this.div.appendChild(img);

  // Add the element to the "overlayLayer" pane.
  const panes = this.getPanes();

  panes.overlayLayer.appendChild(this.div);
}

ওভারলে আঁকুন

মনে রাখবেন যে উপরের কোডে আমরা কোনও বিশেষ ভিজ্যুয়াল ডিসপ্লে ব্যবহার করিনি। API যখনই ম্যাপে ওভারলে আঁকতে হবে, প্রথমবার যোগ করার সময় সহ, ওভারলেতে একটি পৃথক draw() পদ্ধতি ব্যবহার করে।

তাই আমরা এই draw() পদ্ধতিটি বাস্তবায়ন করব, getProjection() ব্যবহার করে ওভারলে'র MapCanvasProjection পুনরুদ্ধার করব এবং বস্তুর উপরের ডান এবং নীচের বাম বিন্দুগুলিকে অ্যাঙ্কর করার জন্য সঠিক স্থানাঙ্ক গণনা করব। তারপর আমরা <div> এর আকার পরিবর্তন করতে পারি। এর ফলে ওভারলে'র কনস্ট্রাক্টরে আমরা যে সীমানা উল্লেখ করেছি তার সাথে মেলে চিত্রটির আকার পরিবর্তন হবে।

টাইপস্ক্রিপ্ট

draw() {
  // We use the south-west and north-east
  // coordinates of the overlay to peg it to the correct position and size.
  // To do this, we need to retrieve the projection from the overlay.
  const overlayProjection = this.getProjection();

  // Retrieve the south-west and north-east coordinates of this overlay
  // in LatLngs and convert them to pixel coordinates.
  // We'll use these coordinates to resize the div.
  const sw = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getSouthWest()
  )!;
  const ne = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getNorthEast()
  )!;

  // Resize the image's div to fit the indicated dimensions.
  if (this.div) {
    this.div.style.left = sw.x + "px";
    this.div.style.top = ne.y + "px";
    this.div.style.width = ne.x - sw.x + "px";
    this.div.style.height = sw.y - ne.y + "px";
  }
}

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

draw() {
  // We use the south-west and north-east
  // coordinates of the overlay to peg it to the correct position and size.
  // To do this, we need to retrieve the projection from the overlay.
  const overlayProjection = this.getProjection();
  // Retrieve the south-west and north-east coordinates of this overlay
  // in LatLngs and convert them to pixel coordinates.
  // We'll use these coordinates to resize the div.
  const sw = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getSouthWest(),
  );
  const ne = overlayProjection.fromLatLngToDivPixel(
    this.bounds.getNorthEast(),
  );

  // Resize the image's div to fit the indicated dimensions.
  if (this.div) {
    this.div.style.left = sw.x + "px";
    this.div.style.top = ne.y + "px";
    this.div.style.width = ne.x - sw.x + "px";
    this.div.style.height = sw.y - ne.y + "px";
  }
}

একটি কাস্টম ওভারলে সরান

ম্যাপ থেকে ওভারলে পরিষ্কারভাবে মুছে ফেলার জন্য আমরা একটি onRemove() পদ্ধতিও যোগ করি।

টাইপস্ক্রিপ্ট

/**
 * The onRemove() method will be called automatically from the API if
 * we ever set the overlay's map property to 'null'.
 */
onRemove() {
  if (this.div) {
    (this.div.parentNode as HTMLElement).removeChild(this.div);
    delete this.div;
  }
}

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

/**
 * The onRemove() method will be called automatically from the API if
 * we ever set the overlay's map property to 'null'.
 */
onRemove() {
  if (this.div) {
    this.div.parentNode.removeChild(this.div);
    delete this.div;
  }
}

একটি কাস্টম ওভারলে লুকান এবং দেখান

যদি আপনি কোনও ওভারলে তৈরি বা অপসারণের পরিবর্তে লুকাতে বা দেখাতে চান, তাহলে ওভারলেটির দৃশ্যমানতা সামঞ্জস্য করার জন্য আপনি নিজস্ব hide() এবং show() পদ্ধতি প্রয়োগ করতে পারেন। বিকল্পভাবে, আপনি মানচিত্রের DOM থেকে ওভারলেটি আলাদা করতে পারেন, যদিও এই অপারেশনটি কিছুটা ব্যয়বহুল। মনে রাখবেন যে আপনি যদি মানচিত্রের DOM-এ ওভারলেটি পুনরায় সংযুক্ত করেন, তাহলে এটি ওভারলেটির onAdd() পদ্ধতিটি পুনরায় ব্যবহার করবে।

নিচের উদাহরণে ওভারলে'র প্রোটোটাইপে hide() এবং show() পদ্ধতি যোগ করা হয়েছে যা <div> কন্টেইনারের দৃশ্যমানতা টগল করে। অতিরিক্তভাবে, আমরা একটি toggleDOM() পদ্ধতি যোগ করি, যা ওভারলেটিকে ম্যাপে/থেকে সংযুক্ত বা বিচ্ছিন্ন করে।

টাইপস্ক্রিপ্ট

/**
 *  Set the visibility to 'hidden' or 'visible'.
 */
hide() {
  if (this.div) {
    this.div.style.visibility = "hidden";
  }
}

show() {
  if (this.div) {
    this.div.style.visibility = "visible";
  }
}

toggle() {
  if (this.div) {
    if (this.div.style.visibility === "hidden") {
      this.show();
    } else {
      this.hide();
    }
  }
}

toggleDOM(map: google.maps.Map) {
  if (this.getMap()) {
    this.setMap(null);
  } else {
    this.setMap(map);
  }
}

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

/**
 *  Set the visibility to 'hidden' or 'visible'.
 */
hide() {
  if (this.div) {
    this.div.style.visibility = "hidden";
  }
}
show() {
  if (this.div) {
    this.div.style.visibility = "visible";
  }
}
toggle() {
  if (this.div) {
    if (this.div.style.visibility === "hidden") {
      this.show();
    } else {
      this.hide();
    }
  }
}
toggleDOM(map) {
  if (this.getMap()) {
    this.setMap(null);
  } else {
    this.setMap(map);
  }
}

বোতাম নিয়ন্ত্রণ যোগ করুন

toggle এবং toggleDom পদ্ধতিগুলি ট্রিগার করতে, মানচিত্রে বোতাম নিয়ন্ত্রণ যোগ করা হয়।

টাইপস্ক্রিপ্ট

const toggleButton = document.createElement("button");

toggleButton.textContent = "Toggle";
toggleButton.classList.add("custom-map-control-button");

const toggleDOMButton = document.createElement("button");

toggleDOMButton.textContent = "Toggle DOM Attachment";
toggleDOMButton.classList.add("custom-map-control-button");

toggleButton.addEventListener("click", () => {
  overlay.toggle();
});

toggleDOMButton.addEventListener("click", () => {
  overlay.toggleDOM(map);
});

map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);

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

const toggleButton = document.createElement("button");

toggleButton.textContent = "Toggle";
toggleButton.classList.add("custom-map-control-button");

const toggleDOMButton = document.createElement("button");

toggleDOMButton.textContent = "Toggle DOM Attachment";
toggleDOMButton.classList.add("custom-map-control-button");
toggleButton.addEventListener("click", () => {
  overlay.toggle();
});
toggleDOMButton.addEventListener("click", () => {
  overlay.toggleDOM(map);
});
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);

সম্পূর্ণ নমুনা কোড

সম্পূর্ণ নমুনা কোডটি নীচে দেওয়া হল:

টাইপস্ক্রিপ্ট

// This example adds hide() and show() methods to a custom overlay's prototype.
// These methods toggle the visibility of the container <div>.
// overlay to or from the map.

function initMap(): void {
  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 11,
      center: { lat: 62.323907, lng: -150.109291 },
      mapTypeId: "satellite",
    }
  );

  const bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(62.281819, -150.287132),
    new google.maps.LatLng(62.400471, -150.005608)
  );

  // The photograph is courtesy of the U.S. Geological Survey.
  let image = "https://developers.google.com/maps/documentation/javascript/";

  image += "examples/full/images/talkeetna.png";

  /**
   * The custom USGSOverlay object contains the USGS image,
   * the bounds of the image, and a reference to the map.
   */
  class USGSOverlay extends google.maps.OverlayView {
    private bounds: google.maps.LatLngBounds;
    private image: string;
    private div?: HTMLElement;

    constructor(bounds: google.maps.LatLngBounds, image: string) {
      super();

      this.bounds = bounds;
      this.image = image;
    }

    /**
     * onAdd is called when the map's panes are ready and the overlay has been
     * added to the map.
     */
    onAdd() {
      this.div = document.createElement("div");
      this.div.style.borderStyle = "none";
      this.div.style.borderWidth = "0px";
      this.div.style.position = "absolute";

      // Create the img element and attach it to the div.
      const img = document.createElement("img");

      img.src = this.image;
      img.style.width = "100%";
      img.style.height = "100%";
      img.style.position = "absolute";
      this.div.appendChild(img);

      // Add the element to the "overlayLayer" pane.
      const panes = this.getPanes()!;

      panes.overlayLayer.appendChild(this.div);
    }

    draw() {
      // We use the south-west and north-east
      // coordinates of the overlay to peg it to the correct position and size.
      // To do this, we need to retrieve the projection from the overlay.
      const overlayProjection = this.getProjection();

      // Retrieve the south-west and north-east coordinates of this overlay
      // in LatLngs and convert them to pixel coordinates.
      // We'll use these coordinates to resize the div.
      const sw = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getSouthWest()
      )!;
      const ne = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getNorthEast()
      )!;

      // Resize the image's div to fit the indicated dimensions.
      if (this.div) {
        this.div.style.left = sw.x + "px";
        this.div.style.top = ne.y + "px";
        this.div.style.width = ne.x - sw.x + "px";
        this.div.style.height = sw.y - ne.y + "px";
      }
    }

    /**
     * The onRemove() method will be called automatically from the API if
     * we ever set the overlay's map property to 'null'.
     */
    onRemove() {
      if (this.div) {
        (this.div.parentNode as HTMLElement).removeChild(this.div);
        delete this.div;
      }
    }

    /**
     *  Set the visibility to 'hidden' or 'visible'.
     */
    hide() {
      if (this.div) {
        this.div.style.visibility = "hidden";
      }
    }

    show() {
      if (this.div) {
        this.div.style.visibility = "visible";
      }
    }

    toggle() {
      if (this.div) {
        if (this.div.style.visibility === "hidden") {
          this.show();
        } else {
          this.hide();
        }
      }
    }

    toggleDOM(map: google.maps.Map) {
      if (this.getMap()) {
        this.setMap(null);
      } else {
        this.setMap(map);
      }
    }
  }

  const overlay: USGSOverlay = new USGSOverlay(bounds, image);

  overlay.setMap(map);

  const toggleButton = document.createElement("button");

  toggleButton.textContent = "Toggle";
  toggleButton.classList.add("custom-map-control-button");

  const toggleDOMButton = document.createElement("button");

  toggleDOMButton.textContent = "Toggle DOM Attachment";
  toggleDOMButton.classList.add("custom-map-control-button");

  toggleButton.addEventListener("click", () => {
    overlay.toggle();
  });

  toggleDOMButton.addEventListener("click", () => {
    overlay.toggleDOM(map);
  });

  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;

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

// This example adds hide() and show() methods to a custom overlay's prototype.
// These methods toggle the visibility of the container <div>.
// overlay to or from the map.
function initMap() {
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: 11,
    center: { lat: 62.323907, lng: -150.109291 },
    mapTypeId: "satellite",
  });
  const bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(62.281819, -150.287132),
    new google.maps.LatLng(62.400471, -150.005608),
  );
  // The photograph is courtesy of the U.S. Geological Survey.
  let image = "https://developers.google.com/maps/documentation/javascript/";

  image += "examples/full/images/talkeetna.png";
  /**
   * The custom USGSOverlay object contains the USGS image,
   * the bounds of the image, and a reference to the map.
   */
  class USGSOverlay extends google.maps.OverlayView {
    bounds;
    image;
    div;
    constructor(bounds, image) {
      super();
      this.bounds = bounds;
      this.image = image;
    }
    /**
     * onAdd is called when the map's panes are ready and the overlay has been
     * added to the map.
     */
    onAdd() {
      this.div = document.createElement("div");
      this.div.style.borderStyle = "none";
      this.div.style.borderWidth = "0px";
      this.div.style.position = "absolute";

      // Create the img element and attach it to the div.
      const img = document.createElement("img");

      img.src = this.image;
      img.style.width = "100%";
      img.style.height = "100%";
      img.style.position = "absolute";
      this.div.appendChild(img);

      // Add the element to the "overlayLayer" pane.
      const panes = this.getPanes();

      panes.overlayLayer.appendChild(this.div);
    }
    draw() {
      // We use the south-west and north-east
      // coordinates of the overlay to peg it to the correct position and size.
      // To do this, we need to retrieve the projection from the overlay.
      const overlayProjection = this.getProjection();
      // Retrieve the south-west and north-east coordinates of this overlay
      // in LatLngs and convert them to pixel coordinates.
      // We'll use these coordinates to resize the div.
      const sw = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getSouthWest(),
      );
      const ne = overlayProjection.fromLatLngToDivPixel(
        this.bounds.getNorthEast(),
      );

      // Resize the image's div to fit the indicated dimensions.
      if (this.div) {
        this.div.style.left = sw.x + "px";
        this.div.style.top = ne.y + "px";
        this.div.style.width = ne.x - sw.x + "px";
        this.div.style.height = sw.y - ne.y + "px";
      }
    }
    /**
     * The onRemove() method will be called automatically from the API if
     * we ever set the overlay's map property to 'null'.
     */
    onRemove() {
      if (this.div) {
        this.div.parentNode.removeChild(this.div);
        delete this.div;
      }
    }
    /**
     *  Set the visibility to 'hidden' or 'visible'.
     */
    hide() {
      if (this.div) {
        this.div.style.visibility = "hidden";
      }
    }
    show() {
      if (this.div) {
        this.div.style.visibility = "visible";
      }
    }
    toggle() {
      if (this.div) {
        if (this.div.style.visibility === "hidden") {
          this.show();
        } else {
          this.hide();
        }
      }
    }
    toggleDOM(map) {
      if (this.getMap()) {
        this.setMap(null);
      } else {
        this.setMap(map);
      }
    }
  }

  const overlay = new USGSOverlay(bounds, image);

  overlay.setMap(map);

  const toggleButton = document.createElement("button");

  toggleButton.textContent = "Toggle";
  toggleButton.classList.add("custom-map-control-button");

  const toggleDOMButton = document.createElement("button");

  toggleDOMButton.textContent = "Toggle DOM Attachment";
  toggleDOMButton.classList.add("custom-map-control-button");
  toggleButton.addEventListener("click", () => {
    overlay.toggle();
  });
  toggleDOMButton.addEventListener("click", () => {
    overlay.toggleDOM(map);
  });
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleDOMButton);
  map.controls[google.maps.ControlPosition.TOP_RIGHT].push(toggleButton);
}

window.initMap = initMap;

সিএসএস

/* 
 * 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;
}

.custom-map-control-button {
  background-color: #fff;
  border: 0;
  border-radius: 2px;
  box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);
  margin: 10px;
  padding: 0 0.5em;
  font: 400 18px Roboto, Arial, sans-serif;
  overflow: hidden;
  height: 40px;
  cursor: pointer;
}
.custom-map-control-button:hover {
  background: rgb(235, 235, 235);
}

এইচটিএমএল

<html>
  <head>
    <title>Showing/Hiding Overlays</title>

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div id="map"></div>

    <!-- 
      The `defer` attribute causes the script to execute after the full HTML
      document has been parsed. For non-blocking uses, avoiding race conditions,
      and consistent behavior across browsers, consider loading using Promises. See
      https://developers.google.com/maps/documentation/javascript/load-maps-js-api
      for more information.
      -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly"
      defer
    ></script>
  </body>
</html>

নমুনা চেষ্টা করুন